很多教程用到了
let cell = collectionView.cellForItem(at: indexPath)
这个其实是不对的,因为这个只适合于背景变色,而且数量不能太多,不能超过CollectionView的缓存范围,所以,如果要正确使用,我建议各位在数据模型中改变!
在数据模型中设立Selected:Bool
然后在返回Cell中进行判断
利用
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { Tags[indexPath.row].selected = true } func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) { Tags[indexPath.row].selected = false tagscolltionView?.reloadData()
这样才能够真正的改变字体颜色,切在重用Cell的时候不会乱。