suitescripter123
suitescripter123

Reputation: 65

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?

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?

Here is my code:

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

}

Upvotes: 0

Views: 81

Answers (1)

Alexey Pichukov
Alexey Pichukov

Reputation: 3405

You can do it with delegate mechanism

Create protocol for your action:

 protocol LikeProtocol {
        func likeOrDislikeActionAtRow(row: Int)
    }

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()
        ...
    }
    ...
}

Add to your PFTableViewCell delegate object with type on protocol and row variable:

var delegate: LikeProtocol?
var rowValue: Int?

Set delegate and row at your 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!)
    ....
}

Upvotes: 1

Related Questions