【发布时间】:2018-09-23 05:23:09
【问题描述】:
这是我希望单元格的样子,
我正在做 indexPath.row % 3 == 0 但不幸的是这不起作用。
【问题讨论】:
标签: swift user-interface layout collectionview
这是我希望单元格的样子,
我正在做 indexPath.row % 3 == 0 但不幸的是这不起作用。
【问题讨论】:
标签: swift user-interface layout collectionview
它与collection view layout无关,它是关于你想如何显示单元格。
我建议在collectionView(_:willDisplay:forItemAt:) 中执行此逻辑,如下所示:
假设你的视图控制器符合UICollectionViewDelegate:
func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
cell.backgroundColor = [.purple, .white, .white, .purple][indexPath.item % 4]
}
感谢 @vacawama 优雅的编辑 ([.purple, .white, .white, .purple][indexPath.item % 4])。
【讨论】:
cell.backgroundColor = [.purple, .white, .white, .purple][indexPath.item % 4]
cell.backgroundColor = [.purple, .white, .white, .purple][indexPath.item % 4]
感谢@vacawama
【讨论】: