Changing cell background color in UICollectionView in Swift

前端 未结 3 1020
执笔经年
执笔经年 2020-12-09 11:48

I am using horizontal collection view to scroll dates. Collection view contain 30 cells. If I select first cell, to indicate the selection, cell background color has been ch

3条回答
  •  臣服心动
    2020-12-09 12:33

    You could maintain a copy of the last selected index path, and then in your didSelectItemAtIndexPath compare the index paths to see if they are different. If different, change the colors of the two cells at those index paths as necessary and then copy the new index path over the old.


    Edit

    Thinking about this again, this should be done with the backgroundView and selectedBackgroundView properties of the cells. After you dequeue a cell you can do the following to let iOS handle the changing.

    cell.backgroundView.backgroundColor = [UIColor redColor];
    cell.selectedBackgroundView.backgroundColor = [UIColor brownColor];
    

提交回复
热议问题