Swift:numberOfSectionsInCollectionView 不工作?
Swift: numberOfSectionsInCollectionView not working?
好的,我在 collection 视图上遇到了问题。这就是我试图实现的目标 - 1 个大的 MAIN header 和页脚,然后嵌套在里面需要一些数字(例如:4)部分,它们都只有 HEADERS。像这样:
header 主要
------
tiny header 1
------
[a][b][c]
------
tiny header 2
------
[a][b][c]
页脚主要
看到类似的答案,我首先尝试创建不同的小节,因为现在我只有所有 collection 视图单元格和一个大页脚和 header。问题是无论我有什么,我只剩下 1 个部分:
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 4
}
我不知道为什么。我从情节提要中获取 collection 视图并将其设置为:
@IBOutlet var collectionView: UICollectionView!
self.collectionView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0) //-44
self.collectionView.backgroundColor = UIColor.white
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
switch kind {
case UICollectionElementKindSectionHeader:
let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "headerCell", for: indexPath as IndexPath) as! HeaderCollectionReusableView
header = headerView
return headerView
case UICollectionElementKindSectionFooter:
let footerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "footerCell", for: indexPath as IndexPath) as! FooterCollectionReusableView
//footerView.center = CGPoint(x: footerView.center.x, y: footerView.center.y + 100)
return footerView
default:
assert(false, "Unexpected element kind")
}
}
// ADD STICKER TO CELL
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "stickerCell", for: indexPath as IndexPath) as! StickerCollectionViewCell
cell.backgroundColor = UIColor.blue
cell.initSticker(stickerView: stickerPack[indexPath.item])
cell.stickerName = (stickerPack[indexPath.item].sticker?.localizedDescription)!
cell.initLabels()
stickerNames.append(cell.stickerName)
cell.hasSticker = true;
//HERE MODIFY SPACING ---------------------------------------
//then for overflow just increase bottom inset?
//cell.center = CGPoint(x: cell.center.x, y: cell.center.y+100)
return cell
}
我该怎么做?为什么当我有 4 个元素要进入单元格时我只有 1 个部分,而当我将 "number items in section" 设置为 4 时它们都显示在那里?
我做错了什么?
Problem is no matter what I have for this, I remain with only 1 section.
这是因为在Swift3中需要去掉方法名的后缀部分,只留下numberOfSections
,即
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 4
}
好的,我在 collection 视图上遇到了问题。这就是我试图实现的目标 - 1 个大的 MAIN header 和页脚,然后嵌套在里面需要一些数字(例如:4)部分,它们都只有 HEADERS。像这样:
header 主要
------
tiny header 1
------
[a][b][c]
------
tiny header 2
------
[a][b][c]
页脚主要
看到类似的答案,我首先尝试创建不同的小节,因为现在我只有所有 collection 视图单元格和一个大页脚和 header。问题是无论我有什么,我只剩下 1 个部分:
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 4
}
我不知道为什么。我从情节提要中获取 collection 视图并将其设置为:
@IBOutlet var collectionView: UICollectionView!
self.collectionView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0) //-44
self.collectionView.backgroundColor = UIColor.white
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
switch kind {
case UICollectionElementKindSectionHeader:
let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "headerCell", for: indexPath as IndexPath) as! HeaderCollectionReusableView
header = headerView
return headerView
case UICollectionElementKindSectionFooter:
let footerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "footerCell", for: indexPath as IndexPath) as! FooterCollectionReusableView
//footerView.center = CGPoint(x: footerView.center.x, y: footerView.center.y + 100)
return footerView
default:
assert(false, "Unexpected element kind")
}
}
// ADD STICKER TO CELL
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "stickerCell", for: indexPath as IndexPath) as! StickerCollectionViewCell
cell.backgroundColor = UIColor.blue
cell.initSticker(stickerView: stickerPack[indexPath.item])
cell.stickerName = (stickerPack[indexPath.item].sticker?.localizedDescription)!
cell.initLabels()
stickerNames.append(cell.stickerName)
cell.hasSticker = true;
//HERE MODIFY SPACING ---------------------------------------
//then for overflow just increase bottom inset?
//cell.center = CGPoint(x: cell.center.x, y: cell.center.y+100)
return cell
}
我该怎么做?为什么当我有 4 个元素要进入单元格时我只有 1 个部分,而当我将 "number items in section" 设置为 4 时它们都显示在那里?
我做错了什么?
Problem is no matter what I have for this, I remain with only 1 section.
这是因为在Swift3中需要去掉方法名的后缀部分,只留下numberOfSections
,即
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 4
}