如何在我的集合視圖的didSelectItemAtIndexPath中設置任何單元格的值。所以我想將所有其他單元格的邊框顏色更改爲黑色,以期望用戶單擊的單元格。如何訪問UICollectionViewController(Swift)中的method didSelectItemAtIndexPath()的所有單元格
謝謝
如何在我的集合視圖的didSelectItemAtIndexPath中設置任何單元格的值。所以我想將所有其他單元格的邊框顏色更改爲黑色,以期望用戶單擊的單元格。如何訪問UICollectionViewController(Swift)中的method didSelectItemAtIndexPath()的所有單元格
謝謝
您可以通過保持選擇和指標的參考實現這一
var isSelected:Bool = false;
var index: Int;
和didSelectItemAtIndexPath改變IsSelected屬性爲true,並保證指數路徑行值索引然後重新集合視圖
當您填充您的單元格時,添加條件
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
if(isSelected && indexPath.Row != index)
{
// change the color to the black
}
}
下面的代碼遍歷的實現代碼如下部分,每個部分的行。它將每行的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
}
}
}