Выбор ячеек изменяется при прокрутке UICollectionView. Ниже то, что я делаю, пожалуйста, проверьте код, упомянутый ниже. Я выбираю ячейки в разделе 0, которые имеют несколько ячеек, и в разделе 1, которые имеют только одну ячейку. Таким образом, при выборе ячейки раздела 1 все выборки, сделанные в разделе 0, будут понятны. Эта функция работает, но при выборе прокрутки получает изменения.
func initUI()
{
self.collectionViewBrands!.registerClass(SignupStep3CollectionViewCell.self, forCellWithReuseIdentifier: "cell")
let nibName = UINib(nibName: "SignupStep3CollectionViewCell", bundle:nil)
self.collectionViewBrands.registerNib(nibName, forCellWithReuseIdentifier: "cell")
self.collectionViewBrands.allowsMultipleSelection = true
}
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 2
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if section == 0
{
return self.arrItems.count
}
else
{
return 1
}
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets {
if section == 1
{
let flowLayout = (collectionViewLayout as! UICollectionViewFlowLayout)
let cellSpacing = flowLayout.minimumInteritemSpacing
let cellWidth = flowLayout.itemSize.width
let cellCount = CGFloat(collectionView.numberOfItemsInSection(section))
let totalCellWidth = cellWidth * cellCount
let totalSpacingWidth = cellSpacing * (cellCount - 1)
let leftInset = (self.collectionViewBrands.frame.size.width - CGFloat(totalCellWidth + totalSpacingWidth)) / 2;
let rightInset = leftInset
return UIEdgeInsetsMake(0, leftInset, 0, rightInset)
}
else
{
return UIEdgeInsetsMake(0, 0, 28, 0)
}
}
// make a cell for each cell index path
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
// get a reference to our storyboard cell
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! SignupStep3CollectionViewCell
// Use the outlet in our custom class to get a reference to the UILabel in the cell
if indexPath.section == 0
{
cell.contentView.backgroundColor = UIColor.whiteColor()
cell.lblNone.hidden = true
cell.imgBrands.backgroundColor = UIColor.clearColor()
cell.imgBrands.image = UIImage(named: "logo_sample")
}
else
{
cell.contentView.backgroundColor = UIColor.clearColor()
cell.imgBrands.image = UIImage(named: "")
cell.lblNone.hidden = false
cell.lblNone.backgroundColor = UIColor.clearColor()
cell.lblNone.text = "None"
}
return cell
}
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath){
let cell = collectionView.cellForItemAtIndexPath(indexPath) as! SignupStep3CollectionViewCell
cell.layer.borderWidth = 4
cell.layer.masksToBounds = false
cell.layer.borderColor = UIColor.init(red: 46.0/255.0, green: 234.0/255.0, blue: 219.0/255.0, alpha: 1.0).CGColor
cell.layer.cornerRadius = cell.frame.height/2
cell.clipsToBounds = true
if indexPath.section == 0
{
self.arrSelectedItems.addObject(self.arrItems.objectAtIndex(indexPath.row))
let indexSet = NSIndexSet(index: 1)
self.collectionViewBrands.reloadSections(indexSet)
}
if indexPath.section == 1
{
let indexSet = NSIndexSet(index: 0)
self.arrSelectedItems.removeAllObjects()
self.collectionViewBrands.reloadSections(indexSet)
}
}
Пожалуйста, руководство, что неправильно выше. И не стесняйтесь спрашивать, если что-то не ясно.
Проблема заключается в том , что из-за механизма повторного использования ячейки
UICollectionView
вы не гарантируете, что вы получите тот же экземпляр ячейки по заданному пути индекса, таким образом, конфигурация, которую вы сделали,didSelectItemAtIndexPath
может потеряться.Вы можете решить эту проблему, настроив внешний вид выбора ячейки
cellForItemAtIndexPath
: