【发布时间】:2015-06-25 13:21:55
【问题描述】:
我有一个带有自定义单元格的 TableViewController。当我在其中一个单元格内点击类似按钮时,会导致至少另一个单元格点击类似按钮。
我正在使用 Parse,它不会影响第二个被幽灵点击的实际点赞数,但它会禁用点赞按钮并将其变为红色。
我已阅读有关单元重用和类似主题的信息,但完全迷失了方向。我是 swift 新手,如果有人可以帮助我解决这个问题,我在 SO 上找不到关于 Swift 和 Parse 的解决方案。
TableViewController
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell:ChinTwoTableViewCell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! ChinTwoTableViewCell
cell.selectionStyle = .None
// Configure the cell...
let chinTwo:PFObject = self.timelineData.objectAtIndex(indexPath.row) as! PFObject
var myVar:Int = chinTwo.objectForKey("likeCount") as! Int
cell.countLabel.text = String(myVar)
cell.nameLabel.text = chinTwo.objectForKey("name") as? String
cell.bodyText.text = chinTwo.objectForKey("body") as! String
cell.bodyText.font = UIFont(name: "HelveticaNeue-UltraLight", size: 18)
cell.bodyText.textAlignment = .Center
cell.likeButton.tag = indexPath.row;
cell.likeButton.addTarget(self, action: "likeButtonTapped:", forControlEvents: .TouchUpInside)
return cell
}
@IBAction func likeButtonTapped(sender: AnyObject) {
let chinTwo = self.timelineData[sender.tag] as! PFObject
chinTwo["likeCount"] = (chinTwo["likeCount"] as! Int) + 1
sender.setTitleColor(UIColor.redColor(), forState: UIControlState.Normal)
chinTwo.saveInBackgroundWithBlock {
(success: Bool, error: NSError?) -> Void in
if (success) {
println("Worked")
} else {
println("Didn't Work")
}
}
self.tableView.reloadData()
}
TableViewCell
@IBAction func likeTapped(sender: AnyObject) {
likeButton.enabled = false
}
报告按钮也会出现同样的问题。
【问题讨论】:
-
两件事:(1)请发布您的 cellForRowAtIndexPath 代码(您的 IBAction 中似乎有真正属于那里的代码)。 (2) 这不是您的问题,但是...您不希望每次都重新加载数据,除非对一个单元格的操作确实会影响整个数据集。随着您的 tableView 数据集的增长,这将导致一个可笑的负担。查看 reloadRowsAtIndexPaths。
-
这不是您的问题的确切答案,但我在这里回答的问题可能会开始为您指明正确的方向:stackoverflow.com/questions/31016503/…
-
@BradBrighton 我发布了 cellForRowAtIndexPatch,所以我应该删除该行以进行 reloadData 吗?我这样做的唯一原因是因为当我按下点赞按钮时,点赞计数器在 Parse 上会上升,但在 tableview 上却没有。有什么方法可以双向更新而不需要重新加载整个数据集?
-
我会看看新代码......另外,我特别提到了一个调用来查看重新加载:reloadRowsAtIndexPaths()。
-
我尝试添加以下内容,但似乎没有奏效。 var selectedRowIndex = self.tableView.indexPathForSelectedRow() if let indexPath = selectedRowIndex { self.tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.None) }
标签: ios swift uitableview cocoa-touch parse-platform