1
我已經使用UICollectionView顯示了單元格(0到9和確定,取消按鈕)。如何使用Swift在UICollectionView中顯示/隱藏單元格?
下面是我想要的東西:
- 確定和取消按鈕,將首先被隱藏。
- 當用戶選擇至少一個號碼時,取消按鈕變得可見。
- 當用戶選擇總共四個號碼,然後確定按鈕也變得可見。
下面是我做的代碼:
var items = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "Cancel","0", "OK"]
...
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! CustomCollectionViewCell
cell.lblNumber!.text = self.items[indexPath.item]
if (self.items[indexPath.item])=="Cancel" {
cell.hidden = true;
}
if (self.items[indexPath.item])=="OK" {
cell.hidden = true;
}
return cell
}
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
print("You selected cell #\(indexPath.item) and value : \(self.items[indexPath.item]) count : \(counter)")
...
}
如何才能實現這一目標?這裏
保存在cellForItemAtIndexPath方法將取消和OK鍵細胞引用。您可以將其'contentView'設置爲隱藏。然後在didSelectItemAtIndexPath中根據所選項目的數量,可以使用已保存單元格的引用來隱藏/取消隱藏OK/Cancel按鈕的contentView。 – 7vikram7
另一種方法是保持選擇列表(在'didSelect..'上)並強制更新(通過'reloadData()'),根據列表的數量啓用/禁用取消/ OK。順便說一下,imo啓用/禁用按鈕對用戶來說要比顯示/隱藏更加直觀。 – Alladinian