- 斯威夫特,沒有故事板,UICollectionView從XIB文件
我正在非常簡單的代碼更新單元格的背景顏色,但更多的則一個細胞更新。UICollectionView didSelectItemAtIndexPath影響多個單元格 - 迅速
override func viewDidLoad(){
super.viewDidLoad()
// getting images from library
images = PHAsset.fetchAssetsWithMediaType(.Image, options: nil)
collectionView.allowsMultipleSelection = false
var nipName2 = UINib(nibName: "CollectionViewCell", bundle:nil)
collectionView.registerNib(nipName2, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "HeaderCell")
var nipName = UINib(nibName: "MyViewCell", bundle:nil)
collectionView.registerNib(nipName, forCellWithReuseIdentifier: "Cell")
}
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return images.count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell{
var cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! MyViewCell
cell.frame.size.width = 60
return cell
}
func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView{
switch kind
{
case UICollectionElementKindSectionHeader:
let headerView = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "HeaderCell", forIndexPath: indexPath) as! CollectionViewCell
return headerView
default:
assert(false, "Unexpected element kind")
}
}
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath)
{
var cell = collectionView.cellForItemAtIndexPath(indexPath)
cell?.backgroundColor = UIColor.redColor()
}
我有大約300個不同的細胞。我想更新第三個,但隨機更改其他單元的背景。
我建議覆蓋你的'UICollectionViewCell'的'setSelected:animated:'。 – Larme
您是否允許在'UICollectionView'中進行多項選擇? –
- 「collectionView.multipleTouchEnabled = false」不起作用,如果這是你的意思。 - 「collectionView.allowsMultipleSelection = false」也不起作用。 – fatihyildizhan