컬렉션보기, 배열 및 사전에 대한 내 머리를 얻으려고합니다. 나는 배열에서 이미지를 채울 UIImageView 콘센트가 들어있는 CollectionViewCell이라는 클래스가 있습니다. 문제는 다른 콘텐츠가있는 여러 섹션이 있으므로 이미지를 저장하는 여러 개의 배열을 만들었습니다. 현재 코드에서 배열 인덱스가 범위를 벗어났다는 오류가 발생합니다. 정보를 키와 값으로 구분하는 사전으로 다른 섹션을 채워야합니까?컬렉션보기, 배열 및 사전
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as CollectionViewCell
// Configure the cell
cell.imageView.image = green[indexPath.row]
cell.imageView.image = blue[indexPath.row]
return cell
어떻게 배열에서 채워지는 헤더와 다른 섹션의 이름을 지정할 수 있습니다? names는 문자열 배열이고 HeaderView는 빈 레이블을 포함하는 클래스입니다. 이 코드를 사용하면 오류가 발생합니다.
는override func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
switch kind {
case UICollectionElementKindSectionHeader:
let headerView =
collectionView.dequeueReusableSupplementaryViewOfKind(kind,
withReuseIdentifier: "HeaderView",
forIndexPath: indexPath)
as HeaderView
headerView.label.text = names[indexPath.row]
return headerView
default:
assert(false, "Unexpected element kind")
}
}
는
는 [편집] 섹션 코드 섹션 및 항목 수의 수 :
그래서 녹색 반대로 파란색 섹션의 항목 다른 번호가 있다면override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
//Return the number of sections
return 2
}
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
//Return the number of items in the section
return green.count
}
, 나는 수익을 포함 할 numberOfItemsInSection 함수에서도 blue.count를 사용할 수 있습니까? blue
이 짧은 배열에 이상이있는 경우, 현재 코드에서
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
//Return the number of items in the section
if section == 0 {
return green.count
} else {
return blue.count
}
}
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as CollectionViewCell
// Configure the cell
if indexPath.section == 0 {
cell.imageView.image = green[indexPath.row]
} else {
cell.imageView.image = blue[indexPath.row]
}
return cell
}
: green
및 blue
이후
녹색과 파란색이 서로 다른 셀을 채우고 있습니까? 또한 collectionView : numberOfItemsInSection : 및 numberOfSectionsInCollectionView : code를 게시 할 수 있습니까? –
예! 녹색과 파란색은 배열에 다른 요소가 있습니다. – Beatle