在单元格中使用一个按钮来增加该特定细胞的值

问题描述 投票:0回答:1

我怎样才能增加特定小区的值?

我看到thisthis岗位。我不能去上班现有(和是“脆”?),前者引起分段错误与功能incrementHandler()decrementHandler()

class cell : UITableViewCell {
     @IBOutlet weak var counter: UILabel
     @IBAction func add (sender: AnyButton) {
          counter.text = String(Int(counter.text) + 1) 
          // find it's corresponding stat value (from other class) and update it
          stats[indexPath].counter = Int(counter.text)
     }
}


class tableView: UITableViewController {
    var stats = [Stat]()
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier(cellID, forIndexPath: indexPath) as! ExpandedControllerCell
        let stat = stats[indexPath.row]
        cell.titleLabel.text = stat.name
        cell.bigCounterLabel.text = String(stat.counter)
        cell.smallCounterLabel.text = String(stat.counter)
        return cell
    }
}
ios swift uitableview
1个回答
1
投票

在你的cellForRowAtIndexPath,设置按钮的标签。然后,在你IBAction为方法,使用标签找出哪个单元的按钮被窃听。递增在数组中的值,然后告诉细胞在该indexPath重新加载本身。 (在的cellForRowAtIndexPath,安装计数器值到细胞中。)

你也可以写你的按钮操作,以便它使用该按钮的frame.origin问按钮属于哪个单元格的表格视图。这是更好,那么脆弱。

见我在this thread答案如何做到这一点的解释。

© www.soinside.com 2019 - 2024. All rights reserved.