当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)
}
}
请指出上面的错误。并随意询问是否有任何不清楚的事情。
答案 0 :(得分:0)
问题在于,由于UICollectionView
的单元重用机制,您不能保证在给定的索引路径上获得相同的单元实例,因此您在{{1}中进行了配置可能会迷路。
您可以通过在didSelectItemAtIndexPath
:
cellForItemAtIndexPath