[英]Swift - Only Update count label in UITableView not all objects when like button is pressed
@IBAction func topButton(sender: UIButton) {
let hitPoint = sender.convertPoint(CGPointZero, toView: self.tableView)
let hitIndex = self.tableView.indexPathForRowAtPoint(hitPoint)
let object = objectAtIndexPath(hitIndex)
object.incrementKey("count")
object.saveInBackground()
self.tableView.reloadData()
//I'm not sure how to change this code to update only the label.
NSLog("Top Index Path \(hitIndex?.row)")
sender.enabled = false
}
该代码用于一个类似按钮,该按钮更新Parse上的计数器,然后刷新tableView中的所有数据。 我想知道如何只更新标签。 标签与“赞”按钮位于同一视图上,并且被称为“计数”,没有引号。 我想要它,这样当我按一下时,TableViewCell中计数标签中的数字会增加,而不是刷新所有tableview单元格。
在每个表格视图单元格中,您必须保留对单元格中标签或标签的引用,并了解单元格层次结构。
对于UIButton和UILabel,您必须要做的(如果需要,甚至可以将动画添加到标签更改中):
let cell = button.superview.superview.superview
cell.labelName.text = String(count + 1)
否则,您也可以这样做,这要干净得多,但不同的是,您只需要重新加载更改后的行即可:
self.tableView.beginUpdates()
self.tableView.reloadRowsAtIndexPaths([hitIndex], withRowAnimation:None
self.tableView.endUpdates()
由于您在UITableViewCell中有单独的按钮,因此我假设您可能有一个带有Swift类的xib。
class MyCell: UITableViewCell {
//Linked to the like button in xib
@IBOutlet weak var likeCountLabel: UILabel!
var likeCount = 0
//Linked to the Touch Up Inside event of like button
@IBAction func likeMe() {
//As this method is in the cell so you do not have worry about
//indexPath.
likeCount++
likeCountLabel.text = String(likeCount)
}
override func prepareForReuse() {
//
}
}
关键是您可以将代码移至自定义UITableViewCell而不是UIViewController中。 这样,您将很容易管理。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.