2015-09-26 60 views
0

我有一个UICollectionView与定制单元格有一个IBAction附加。简而言之,当点击单元格上的UIStepper时,它会递增一个值并显示在单元格上的标签中。UICollectionViewCell IBAction影响多个单元格

这么多我没有太多麻烦工作。问题是,如果我滚动足够多的新单元格加载到内存中,匹配被修改的单元格的索引的单元格将显示与原始单元格相同的递增值。

Cell1 Cell2 Cell3 --scroll-> Cell4 Cell5 Cell6 
    0  1  0    0  1  0 

即使没有更改,单元格5也会匹配单元格2的值。

@IBAction func updateValue(sender: AnyObject) { 
    // Code to check if plus or minus 
    self.valueLabel = value 
} 

我该如何解决这个问题?

编辑:

Cell类

protocol ItemCellProtocol { 
    fun getValue(value: Double, increment: Bool) -> Double 
} 

class CustomCollectionViewCell: UICollectionViewCell { 
    @IBOutlet weak var stepper: UIStepper! 
    @IBOutlet weak var valueLabel : UILabel! 
    @IBOutlet weak var image : UIImage! 

    var value = 0 
    var delegate : ItemCellProtocol? 

    @IBAction func updateQuantity(sender: AnyObject) { 
     // Code to check if plus or minus 
     self.valueLabel = value 

     delegate?.getValue(value, increment: true) 
    } 
} 

CollectionViewController

class CustomCollectionViewController : UICollectionViewController { 
    var overallValue = 0 
    items = [item]() // populated during segue of previous VC 

    // CollectionView Delegate classes 

    func .. cellForItemAtIndexPath { 
    let cell = //instance of CustomCollectionViewCell 

    let cellItem : Item = self.items[indexPath.row] 

    cell.image = cellItem.image 

    cell.delegate = self 

    return cell 
    } 

    func getTotal(value: Double, increment: Bool) -> Double { 
    if increment { 
     self.overallValue += value 
    } else { 
     self.overallValue -= value 
    } 
    return self.total 
    } 
} 

这基本上类目前是如何工作的。我目前没有Xcode在我面前,所以我不能给出完整的代码。

+0

你在哪里存储值你的细胞之前,UI的所有状态?您不能将值存储在单元格本身中ss单元格被重用 – Paulw11

+0

请为您的tableView数据源方法添加代码。还有你正在增加和存储步进值的地方。 –

回答

0

UICollectionViewCellUICollectionView被重用所以在您的自定义单元复位被重用

override func prepareForReuse() { 
    super.prepareForReuse() 
    self.valueLabel.text = "0" 
} 
+0

感谢您的回复@dopcn。但是,一旦它离开视野,不会重置原始单元格的值吗? – Sawyer05

+0

@ Sawyer05是的,它会和这是什么集合视图数据源协议做 – dopcn

相关问题