簡體   English   中英

如何在UICollectionViewController(Swift)中的方法didSelectItemAtIndexPath()處訪問所有單元格

[英]How to get access to all cells at the Method didSelectItemAtIndexPath() in a UICollectionViewController (Swift)

如何在didSelectItemAtIndexPath的集合視圖中設置任何單元格的值。 所以我想將所有其他單元格的邊框顏色更改為黑色,期望用戶單擊該單元格。

謝謝

以下代碼遍歷tableview部分以及每個部分的行。 它將每行的indexPath與所選單元格的indexPath進行比較,如果它們不匹配,則允許您對單元格執行所需的操作。

for (NSInteger i = 0; i < [tableView numberOfSections]; ++i)
{
    for (NSInteger j = 0; j < [tableView numberOfRowsInSection:i]; ++j)
    {
        NSIndexPath *cellIdxPath = [NSIndexPath indexPathForRow:j inSection:i];

        // indexPath represents the currently selected row
        if (indexPath != cellIdxPath)
        {
            UITableViewCell *cell = [tableView cellForRowAtIndexPath:cellIdxPath];
            // do whatever you need to do to the cell
        }
    }
}

您可以通過保留選擇和索引參考來實現

var isSelected:Bool = false;
var index: Int;

並在didSelectItemAtIndexPath中將isselected屬性更改為true並將索引路徑行值保留在index中,然后重新加載集合視圖

當您填充單元格時,請添加條件

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

   if(isSelected && indexPath.Row != index )
   {
      // change the color to the black
   }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM