我希望我的 UICollectionView 中的第一个单元格始终存在并成为一个按钮。我知道如何在点击时检查按钮(只是检查 indexPath.row == 0)。
那么我如何将第一个单元格添加为一次性静态对象呢?类似于 Pages iOS 应用程序的工作方式?
我知道一种方法,如果索引为 0,您可以在普通单元格上调整内容/隐藏等 - 但是必须有更好的方法
最佳答案
设置两个原型(prototype)单元:一个仅用于第一个单元(具有重用标识符,例如 FirstCell
),另一个用于其余单元(具有重用标识符,例如 MainCells
)。然后在cellForItemAtIndexPath
根据 indexPath.item
选择 reuseIdentifier :
func collectionView(collectionView: UICollectionView!, cellForItemAtIndexPath indexPath: NSIndexPath!) -> UICollectionViewCell! {
var reuseIdentifier = (indexPath.item == 0) ? "FirstCell" : "MainCells"
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as UICollectionViewCell
// Configure the cell
return cell
}
更新:(从评论中移出)
请注意,为了避免只替换第一个单元格,您必须增加 numberOfItemsInSection
中的计数。用 1 来计算额外的单元格,然后从 indexPath.item
中减去 1当您填充主单元格时。
关于ios - 第一个单元格静态按钮 - UICollectionViewCell - Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25474444/