每个 TableView 单元格都会更新,而不仅仅是被点击的单元格. [英] every TableView Cell updated instead of just one that is clicked.
问题描述
我有两个按钮(喜欢/不喜欢),它们的工作方式类似于大拇指向上和向下投票系统的风格.但是,当我单击按钮时,每个单元格都会更新.有没有办法解决这个问题,以便只有被点击的人才能更新?
I have two Buttons (like/dislike) that works like thumbs up and down voting system style. When I click on the button though, every cell gets updated. Is there a way to solve this so that only the one that gets clicked updated?
另外,如何重新加载单元格,以便我不必拉刷新来更新 button.setTitle 值的值?
Also, How do I reload the cell so that I don't have to pull to refresh to update the value of the button.setTitle value?
这是我的代码:
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 }
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellIdentifier = "TableViewCell" // your cell identifier name in storyboard
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! PFTableViewCell
cell.likeButton.selected = vote == 1
cell.dislikeButton.selected = vote == -1
cell.likeButton.titleLabel!.text = "\(likeCount)"
cell.dislikeButton.titleLabel!.text = "\(dislikeCount)"
return cell
}
推荐答案
你可以用委托机制来做
为您的操作创建protocol
:
protocol LikeProtocol {
func likeOrDislikeActionAtRow(row: Int)
}
让你的 TableViewController
类确认这个协议:
Make your TableViewController
class confirm this protocol:
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
变量:
Add to your PFTableViewCell
delegate object with type on protocol
and row
variable:
var delegate: LikeProtocol?
var rowValue: Int?
在您的 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) 处设置委托和行
:
cell.delegate = self
cell.row = indexPath.row
在喜欢和不喜欢的动作中调用协议方法:
Call protocol method in your like and dislike actions:
@IBAction func likeButton(sender: UIButton) {
....
delegate!.likeOrDislikeActionAtRow(row!)
....
}
这篇关于每个 TableView 单元格都会更新,而不仅仅是被点击的单元格.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!