When I tap a button, I want the value to update for only that cell.
I'm a bit of a noob so please be easy on me!
I currently have a delegate set in the custom cell that I'm accessing in the tableViewController
as well as Bool values that determine whether the button was pressed or not and if the button should be enabled. The button is by default set as true. I created a column in Parse called isEnabled
that gives the button a default value of true
.
Currently, when I tap the button every 3rd cell changes (meaning it doesn't send an action for only that 1 cell). However, when printing to the logs, the logs say that the value has changed from true to false, only AFTER i scroll back up...
Why doesn't the value change immediately?
Here is my custom cell
code:
protocol CellButtonDelegate {
func buttonClicked(cell : PFTableViewCell)
}
var delegate : CellButtonDelegate?
class FeedTableViewCell: PFTableViewCell {
var delegate : CellButtonDelegate?
internal var buttonEnabled : Bool? {
get {
return happyOutlet.enabled
} set {
happyOutlet.enabled = newValue!
}
}
@IBOutlet weak var happyOutlet: UIButton!
@IBAction func happyBtn(sender: AnyObject) {
if(parseObject != nil) {
if var votes: Int? = parseObject!.objectForKey("votes") as? Int {
votes!++
parseObject!.setObject(votes!, forKey: "votes")
parseObject!.saveInBackground()
votesLabel?.text = "\(votes!)"
// print(votes)
}
}
delegate?.buttonClicked(self)
viewBlackYellow.backgroundColor = UIColor.yellowColor()
happyOutlet.enabled = false
sadOutlet.enabled = false
happyOutlet.alpha = 0
sadOutlet.alpha = 0
//These values all get updated for every 3rd cell
}
Here is my cellForRowAtIndexPath
code (The values for the votes, as well as the other values I have are being cached):
The numberOfRowsInSection appears based on the scroll because the values/cells are being cached as I set the number of cells to return biased on objects which depends on the scroll:
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
objectArray = objects //the objects array is cached (using PFQuery function to query the values, so the cells are cached as well)
return objects!.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject?) -> PFTableViewCell? {
var cell: FeedTableViewCell? = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as? FeedTableViewCell
objects![indexPath.row]
if let pfObject = objectArray?[indexPath.row] {
cell?.parseObject = object
var votes: Int? = pfObject["votes"] as? Int
if votes == nil {
votes = 0
}
cell?.delegate = self
let isEnabled = pfObject["isEnabled"] as! Int
if isEnabled == 1 {
cell?.buttonEnabled = true
} else {
cell?.buttonEnabled = false
print("its false")
}
return cell
}
func buttonClicked(cell: PFTableViewCell) {
let indexPath = tableView.indexPathForCell(cell)
var object : PFObject = objectArray![indexPath!.row] as! PFObject
object["isEnabled"] = 0
objectArray?.removeAtIndex((indexPath?.row)!)
objectArray?.insert(object, atIndex: (indexPath?.row)!)
}
How do I make it so that the cell values and actions don't repeat? I'm assuming that all the logic should not be going in the custom cell but in the tableVC instead. I've tried to implement that however, without any luck (i was sending an action from the happyOutlet.tag).
Lastly, i had the self.tableView.reloadData()
/ self.tableView.reloadRowsAtIndexPaths([indexPath!], withRowAnimation: .None)
Both make the screen jittery because I also have cached video in the cell. It also sometimes (strangely enough) doesn't help. If i can avoid reloading the tableView that would be best.
Any and all suggestions, questions, comments and improvements are immensely appreciated.
I referenced this link as well as others and they have not helped: Implementing a like button in a tableviewCell in swift which didn't help.