我想你可能想保持所選單元格具有不同的背景顏色,對吧? 然後試試這段代碼。
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
cell.backgroundColor = [UIColor magentaColor];
}
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
cell.backgroundColor = [UIColor cyanColor];
}
只是簡單地爲不同狀態的細胞分配不同的BG顏色。 此外,下面的代碼是有人觸摸collectionView單元格時序列觸發方法的文檔。您也可以在UICollectionView.h文件,UICollectionViewDelegate協議部分找到這些文件。
// Methods for notification of selection/deselection and highlight/unhighlight events.
// The sequence of calls leading to selection from a user touch is:
//
// (when the touch begins)
// 1. -collectionView:shouldHighlightItemAtIndexPath:
// 2. -collectionView:didHighlightItemAtIndexPath:
//
// (when the touch lifts)
// 3. -collectionView:shouldSelectItemAtIndexPath: or -collectionView:shouldDeselectItemAtIndexPath:
// 4. -collectionView:didSelectItemAtIndexPath: or -collectionView:didDeselectItemAtIndexPath:
// 5. -collectionView:didUnhighlightItemAtIndexPath:
iosuicollectionviewuicollectionviewdelegate
我一直在尋找這個答案......我改變一個自定義網格UICollectionView和TableView中組合視圖。實現這兩種方法允許選擇並且然後改變選擇。謝謝史蒂夫,這對我來說是個訣竅。 –