【问题标题】:Prevent tableview cell recycle label value防止 tableview 单元格回收标签值
【发布时间】:2016-05-26 03:35:54
【问题描述】:

我有一个包含 1 个label 和 1 个button 的自定义单元格的表格视图。该按钮用于每次点击它,增加label 内的数字,例如如果我的label 值为0,那么每次点击按钮它都会增加1

问题是我有很多数据,例如我有 20 个,每次我为标签设置一个值时,当我滚动时,所有值都会改变

这是我的代码:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier("testCell", forIndexPath: indexPath)

    let pos = cell.viewWithTag(6) as! UIButton

    pos.addTarget(self, action: #selector(testTableViewController.pos(_:)), forControlEvents: .TouchUpInside)
    pos.tag = indexPath.row
    return cell
}

func pos(sender: UIButton) {
    let position: CGPoint = sender.convertPoint(CGPointZero, toView: self.tableView)
    let indexPath = self.tableView.indexPathForRowAtPoint(position)
    let cell: UITableViewCell = self.tableView.cellForRowAtIndexPath(indexPath!)! as UITableViewCell

    let countNo = cell.viewWithTag(7) as! UILabel

    var counter:Int = Int(countNo.text!)!
    counter = counter + 1
    countNo.text = String(counter)
}

请帮我解决这个问题。我已经搜索了很多网络来找到答案,例如 make dequeueReusableCellWithIdentifier nil 但在 Swift 2 dequeueReusableCellWithIdentifier 从来没有 nil 以及更多没有人工作

我需要一些提示如何解决这个问题

【问题讨论】:

    标签: ios swift uitableview uibutton


    【解决方案1】:

    这是你的答案。

    初始化一个对象数与表格视图的行数相同的数组。最初为每个对象插入值“0”或空白字符串(如果最初不想在标签中显示任何数字)。

    例如,

    var arrValues:NSMutableArray = NSMutableArray()
    
        for _ in 1...numberOfrow
    
            {
                arrValues.addObject("0")
            }
    

    现在在 cellForRowAtIndexPath 方法中,显示来自该数组的标签文本

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    
            let cell = tableView.dequeueReusableCellWithIdentifier("testCell", forIndexPath: indexPath)
    
            let pos = cell.viewWithTag(6) as! UIButton
            **let countNo = cell.viewWithTag(7) as! UILabel
            countNo.text = String(arrValues.objectAtIndex(indexPath.row))**
    
            pos.addTarget(self, action: #selector(testTableViewController.pos(_:)), forControlEvents: .TouchUpInside)
            pos.tag = indexPath.row
            return cell
        }
    

    按钮单击方法用递增的数字替换特定索引处的对象。请参阅下面的更新方法。

    func pos(sender: UIButton) {
            let position: CGPoint = sender.convertPoint(CGPointZero, toView: self.tableView)
            let indexPath = self.tableView.indexPathForRowAtPoint(position)
            let cell: UITableViewCell = self.tableView.cellForRowAtIndexPath(indexPath!)! as UITableViewCell
    
            let countNo = cell.viewWithTag(7) as! UILabel
    
            var counter:Int = Int(countNo.text!)!
            counter = counter + 1
            countNo.text = String(counter)
            **arrValues.replaceObjectAtIndex(indexPath.row, withObject: String(counter))**
        }
    

    所以在 cellForRowAtIndexPath 中,它会在标签文本中显示更新后的数字

    【讨论】:

    • 我必须把 for _ in 1...numberOfrow { arrValues.addObject("0") } 放在哪里?如果我把它放在viewDidLoad() 中,我得到了错误
    猜你喜欢
    • 2020-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-19
    • 2013-10-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多