【发布时间】: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