ios - 第一个单元格静态按钮 - UICollectionViewCell - Swift

标签 ios swift uicollectionview cell

我希望我的 UICollectionView 中的第一个单元格始终存在并成为一个按钮。我知道如何在点击时检查按钮(只是检查 indexPath.row == 0)。

那么我如何将第一个单元格添加为一次性静态对象呢?类似于 Pages iOS 应用程序的工作方式?

我知道一种方法,如果索引为 0,您可以在普通单元格上调整内容/隐藏等 - 但是必须有更好的方法

enter image description here

最佳答案

设置两个原型(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/

相关文章:

ios - 导航栏标题字体颜色未使用 RGB 设置,但可以使用标准颜色

ios - 如何在应用程序处于终止状态时在 iOS 设备中接收消息以及如何在打开 wifi/移动数据时提醒应用程序

ios - 获取 UIImageView 中图像的大小

ios - 在 UIViewController 中仅旋转一个 UIView(两个中的一个)

ios - 我可以在 UITableView 完成加载后调用函数吗?

ios - 为什么 UITableViewCell 首先显示以前的值,然后才更新自身?

swift - UICollectionView dequeue 在快速单元测试中引发异常

swift - ProgressBar只显示结束

ios - UICollectionViewFlowLayout 实现

ios - Swift UICollectionView - 从另一个类添加/删除数据