- Swift, без раскадровки, 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 различных ячеек. Я хочу обновить только третье, но случайное изменение фона многих других ячеек.
Я бы предложил переопределить 'setSelected: animated:' вашего 'UICollectionViewCell'. – Larme
Вы разрешаете множественный выбор в 'UICollectionView'? –
- «collectionView.multipleTouchEnabled = false» не работает, если это то, что вы имеете в виду. - «collectionView.allowsMultipleSelection = false» также не работает. – fatihyildizhan