Ios UICollectionView在swift 2中滚动时更改单元格数据
滚动UICollectionView时,单元格的选择会发生更改。下面是我正在做的,请检查下面提到的代码。我选择第0节中有多个单元格的单元格和第1节中只有一个单元格的单元格。因此,在选择第1节单元格时,在第0节中进行的所有选择都将被清除。此功能正在运行,但滚动选择会发生更改Ios UICollectionView在swift 2中滚动时更改单元格数据,ios,swift,uicollectionview,Ios,Swift,Uicollectionview,滚动UICollectionView时,单元格的选择会发生更改。下面是我正在做的,请检查下面提到的代码。我选择第0节中有多个单元格的单元格和第1节中只有一个单元格的单元格。因此,在选择第1节单元格时,在第0节中进行的所有选择都将被清除。此功能正在运行,但滚动选择会发生更改 func initUI() { self.collectionViewBrands!.registerClass(SignupStep3CollectionViewCell.self, forCellWithReuseI
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
中配置单元格选择外观来解决此问题:
let cellIsSelected: Bool = cell.selected
if cellIsSelected {
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
} else {
// set the default layer values, to make sure that a dequeued cell that was marked as selected at the time it was queued resets its look&feel
}