UICollectionView在swift中滚动时更改单元格数据2
问题描述:
当UICollectionView滚动时,单元格的选择会被更改。以下是我正在做的事情,请检查下面提到的代码。我选择第0节中有多个单元的单元和第1节只有一个单元的单元。因此,在选择第1节单元格时,第0节中所做的所有选择都将变得清晰。此功能正在工作,但滚动选择会得到更改。UICollectionView在swift中滚动时更改单元格数据2
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
所做的配置可能会丢失。
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
}
:
可以通过配置小区选择看&感到cellForItemAtIndexPath
解决问题