很多教程用到了
let cell = collectionView.cellForItem(at: indexPath)
這個(gè)其實(shí)是不對(duì)的,因?yàn)檫@個(gè)只適合于背景變色,而且數(shù)量不能太多,不能超過CollectionView的緩存范圍,所以,如果要正確使用,我建議各位在數(shù)據(jù)模型中改變!
在數(shù)據(jù)模型中設(shè)立Selected:Bool
然后在返回Cell中進(jìn)行判斷
利用
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的時(shí)候不會(huì)亂。