我正在制作一个有6 * 7个单元格的集合视图,即使我编写的代码使得我可以在声明UICollectionView
之后更改每个单元格的背景颜色;细胞背景颜色保持白色。其他单元格不会返回白色背景颜色。有人能告诉我为什么只有这个细胞不能保持它的背景颜色吗?
override func viewDidLoad() {
super.viewDidLoad()
let longPressRecognizer = UILongPressGestureRecognizer(target:self, action: #selector(ViewController.longPressAction(_:)))
longPressRecognizer.allowableMovement = 5
longPressRecognizer.minimumPressDuration = 0.5
self.collectionView.addGestureRecognizer(longPressRecognizer)
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let col = indexPath.section
let row = indexPath.row
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("CollectionViewCell", forIndexPath: indexPath) as! TimeTableCollectionViewCell
cell.backgroundColor = UIColor.whiteColor()
if !ViewController.colorDictionary.isEmpty {
for (indexPath, color) in ViewController.colorDictionary {
self.collectionView.cellForItemAtIndexPath(indexPath)?.backgroundColor = color
}
}
答案 0 :(得分:0)
如果有条件,请调试字典。每次从集合视图返回时,它都可能出现在if语句中。