我有以下代码可以更改图片:
func rowA(_ row: Int) -> Bool {
return (row < 1 || (UserDefaults.standard.bool(forKey: "new")))
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! CollectionViewCell
if (indexPath.row == 0) {
cell.cellImageView.image = UIImage(named: "1.png")
} else {
if self.rowA(indexPath.row) {
cell.cellImageView.image = UIImage(named: "1.png")
} else {
cell.cellImageView.image = UIImage(named: "2.png")
}
}
return cell
}
启动我的应用程序后,一切正常。当我开始滚动我的collectionView时,有时第一个单元格(indexPath.row == 0
)会将图像更改为UIImage(named: "2.png")
。如何解决?