我有两个按钮(喜欢/不喜欢),就像拇指向上和向下投票系统风格一样。当我点击按钮时,每个单元格都会更新。有没有办法解决这个问题,以便只有被点击的更新?
另外,如何重新加载单元格以便我不必拉动刷新来更新button.setTitle值?
这是我的代码:
if (affectedRows == 1)
}
PFTableViewCell:
class UserFeedCell: PFTableViewCell {
@IBOutlet weak var likeButton: UIButton!
@IBOutlet weak var dislikeButton: UIButton!
var vote: Int = 0 // initialize to user's existing vote, retrieved from the server
var likeCount: Int = 0 // initialize to existing like count, retrieved from the server
var dislikeCount: Int = 0 // initialize to existing dislike count, retrieved from the server
@IBAction func dislikeButton(sender: UIButton) {
buttonWasClickedForVote(-1)
print(likeCount)
print(dislikeCount)
}
@IBAction func likeButton(sender: UIButton) {
buttonWasClickedForVote(1)
print(likeCount)
print(dislikeCount)
}
private func buttonWasClickedForVote(buttonVote: Int) {
if buttonVote == vote {
// User wants to undo her existing vote.
applyChange(-1, toCountForVote: vote)
vote = 0
}
else {
// User wants to force vote to toggledVote.
// Undo current vote, if any.
applyChange(-1, toCountForVote: vote)
// Apply new vote.
vote = buttonVote
applyChange(1, toCountForVote: vote)
}
}
private func applyChange(change: Int, toCountForVote vote: Int ) {
if vote == -1 { dislikeCount += change }
else if vote == 1 { likeCount += change }
}
答案 0 :(得分:1)
您可以使用委托机制
来完成为您的操作创建protocol
:
protocol LikeProtocol {
func likeOrDislikeActionAtRow(row: Int)
}
让您的TableViewController
班级确认此协议:
class YourTableViewController: UITableViewController, LikeProtocol {
...
func likeOrDislikeActionAtRow(row: Int) {
...
// Reload only you want cell
self.tableView.beginUpdates()
self.tableView.reloadRowsAtIndexPaths([NSIndexPath(forRow: row, inSection: 1)], withRowAnimation: UITableViewRowAnimation.Fade)
self.tableView.endUpdates()
...
}
...
}
使用PFTableViewCell
和protocol
变量上的类型添加到row
委托对象:
var delegate: LikeProtocol?
var rowValue: Int?
在func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath)
:
cell.delegate = self
cell.row = indexPath.row
在你喜欢和不喜欢的行动中调用协议方法:
@IBAction func likeButton(sender: UIButton) {
....
delegate!.likeOrDislikeActionAtRow(row!)
....
}