patrickd
patrickd

Reputation: 285

Redundant image loaded into tableview cell swift

When a user presses a button in my tableview cell, I want to change the image of the button to another image. I do this in the following manner:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("itemCell", forIndexPath: indexPath) as! itemCell
    cell.upBut.tag = indexPath.row
    cell.upBut.addTarget(self, action: "voteUp:", forControlEvents: UIControlEvents.TouchUpInside)
    if arrOfPics[indexPath.row].userLiked != nil
    {
        if arrOfPics[indexPath.row].userLiked == 1
        {
            cell.upBut.setImage(UIImage(named: "green_check.png"), forState: UIControlState.Normal)
            cell.downBut.setImage(UIImage(named: "cross.png"), forState: UIControlState.Normal)
        }
        if arrOfPics[indexPath.row].userLiked == -1
        {
            cell.upBut.setImage(UIImage(named: "checkmark.png"), forState: UIControlState.Normal)
            cell.downBut.setImage(UIImage(named: "red_cross.png"), forState: UIControlState.Normal)
        }

    }
    return cell

}

This method successfully implements the changing of the image of the button in the proper cell that I desire. However, it has a strange side affect: Every third cell also has the same image change. If the button image in cell 1 was changed, then it would also be changed in cell 4, 7, 10, etc.

I found that my representation of the data was not the reason for this, IE arrayOfPics[3].userLiked was nil, but the 4th cell had an updated image.

Also, probably important to know that userLiked is by default nil. It is only set when the user presses on the button whose image we are trying to change, as seen in the following method:

func voteUp(sender:UIButton) { // up 1 --> true, -1 --> false
    NSOperationQueue.mainQueue().addOperationWithBlock {
        if arrOfPics[sender.tag].userLiked != nil && arrOfPics[sender.tag].userLiked == 1 {//let 1 mean user voted up
            return
        }
        dispatch_async(dispatch_get_main_queue()){
            arrOfPics[sender.tag].userLiked = 1
        }
        let request = NSMutableURLRequest(URL: NSURL(string: "http://www.url")!)
        let row = sender.tag
        request.HTTPMethod = "POST"

        let postString = "blahblah"
        request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding)
        let task = NSURLSession.sharedSession().dataTaskWithRequest(request, completionHandler: {
            data, response, error in
            if error != nil {
                print(error)
                return
            }
            dispatch_async(dispatch_get_main_queue()){
                arrOfPics[row].likes!++
                self.tableView.reloadData()
            }
        })

        task.resume()
    }

}

I figured that reloading the tableview data after doing so would cause any relevant cells to see that the object associated it had either been liked or disliked, and adjust its picture accordingly. I am not sure if this strange behavior is due to a race condition, because that does not explain why always every third cell has the same image.

Upvotes: 0

Views: 119

Answers (1)

Gihan
Gihan

Reputation: 2536

if your userLiked is nil may be your dequed cell still has its upBut,downBut images set. Try setting them to nil in else of if arrOfPics[indexPath.row].userLiked != nil

Upvotes: 1

Related Questions