2016-12-30 89 views
0

當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) 
    } 
} 

請指導以上錯誤。如果有任何不清楚的地方,請隨時詢問。

回答

0

的問題是,由於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解決問題

相關問題