ios - 如何识别 UICollectionview 中的特定单元格

标签 ios objective-c uicollectionview

enter image description here

我想为cell1、cell4、cell7、...n设置颜色黑色,为cell2设置颜色白色,单元格5,单元格8,...n 注意:单元格数量不固定。

我该如何实现这个?

最佳答案

我假设您的 cellForItemAtIndexPath 知道如何获取单元格编号(即 123、图片上的 4)从 NSIndexPath 传递给该方法。然后,您可以通过获得除以 3 的余数来决定使用什么颜色,如下所示:

// Decode index path into cell number from your picture
int cellNumber = [self getCellNumberFromIndexPath:indexPath];
// Figure out the color
if (cellNumber % 3 == 1) {
    // 1, 4, 7, etc
    ... // Use black color
} else if (cellNumber % 3 == 2) {
    // 2, 5, 8, etc
    ... // Use white color
}

关于ios - 如何识别 UICollectionview 中的特定单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37762155/

相关文章:

swift - 相对于框架宽度和纵横比的动态 UICollectionViewCell 宽度

iphone - 选项卡推送通知 cocos2d 游戏崩溃

ios - 自动核心数据迁移,如果我在源存储和目标存储之间有一些映射?

objective-c - Swift 无法识别 C 指针数组

objective-c - 如何在 OSX 应用程序中拉伸(stretch)图像?

ios - UITableView 不会转换到下一个 View

ios - 在 viewDidLoad 之前以编程方式将按钮添加到数组中

ios - Xcode 不允许新文件在组中的编辑器中显示

ios - 是否可以使用 eventkit 更改 icalendar 中的默认警报时间?

ios - 为 UICollectionViewController 设置 "float over"控件的最佳方式?