sky*_*guy 1 collectionview ios uicollectionviewcell swift3
好的,我在处理集合视图时遇到了问题。这就是我想要实现的 - 1 个大的 MAIN 页眉和页脚,然后嵌套在里面,需要一些数量(例如:4)个部分都只有 HEADERS。像这样:
------
tiny header 1
------
[a][b][c]
------
tiny header 2
------
[a][b][c]
Run Code Online (Sandbox Code Playgroud)
查看类似的答案,我首先尝试创建不同的小节,因为现在我只有所有集合视图单元格以及一个大页脚和页眉。问题是不管我有什么,我只剩下 1 节:
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 4
}
Run Code Online (Sandbox Code Playgroud)
我不知道为什么。我从情节提要中获取我的收藏视图并将其设置如下:
@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
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?为什么当我有 4 个元素进入单元格时我只有 1 个部分,而当我将“部分中的数字项”设置为 4 时,它们都显示在那里?
我究竟做错了什么?
问题是不管我有什么,我只剩下 1 个部分。
这是因为在 Swift 3 中你需要去掉方法名的后缀部分,只留下numberOfSections,即
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 4
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1510 次 |
| 最近记录: |