很多教程用到了
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的時候不會亂。