1

I have a table view where the user selects friends. My problem is that for some reason when one cell gets selected another unrelated cell gets selected as well. Can anyone help me understand what is happening?

Here is my code for handling the selections:

 override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
     let indexPath = tableView.indexPathForSelectedRow
     let currentCell = tableView.cellForRowAtIndexPath(indexPath!) as! SpotFriendsCell
     let Friend = currentCell.AddFriendLabel.text
     if self.invitedFriends.containsObject(Friend!) {
         invitedFriends.removeObject(Friend!)
         currentCell.Invited.image = UIImage(named: "unckecked.png")
         tableView.reloadData()
     } else {
         invitedFriends.addObject(Friend!)
         NSUserDefaults.standardUserDefaults().setObject("Yes", forKey: "selectedFriends")
         currentCell.Invited.image = UIImage(named: "checked.png")
         tableView.reloadData()
     }
}

I have a UIImageView on the right of the cell that I use to keep track of what friends are selected and are not. If the cell image is "unchecked.png" then the friend is added and the image is changed to "checked.png". and vice versa.

Trip Phillips
  • 430
  • 1
  • 5
  • 18

2 Answers2

2

This line shouldn't be needed as you already have the indexPath in the parameter.

let indexPath = tableView.indexPathForSelectedRow

Then, instead of playing with cells directly, you should play with objects. My guess is that you already have a list containing Friend somewhere, you should get the Friend reference from there selected index.

let selectedFriend = self.allFriends[indexPath.row]

Then you can keep your logic simple.

if self.invitedFriends.containsObject(selectedFriend) {
     invitedFriends.removeObject(selectedFriend)
     } else {
         invitedFriends.addObject(selectedFriend)
         NSUserDefaults.standardUserDefaults().setObject("Yes", forKey: "selectedFriends")
     }

    tableView.reloadData()
}

Your image logic should then simply go inside you cellForRowAtIndexPath method.

Cesare
  • 9,139
  • 16
  • 78
  • 130
  • I am using a PFTableView though so I don't think I can use this. The allFriends is just a helper array so that I can keep track of the friends selected. The table view is being populated by a query. – Trip Phillips Nov 25 '15 at 20:57
  • Ok I figured it out by putting the logic for what the image is in the cellForRowAtIndexPath like you said. Thanks for the help! – Trip Phillips Nov 25 '15 at 21:17
1

When you reloadData, your cells are regenerated from the prototype cell to reuse. If you don't add the functionality to set this image according to your datasource in cellForRowAtIndexPath, the image will always default to the one in your prototype cell.

Martijn
  • 460
  • 4
  • 16
  • So should I try not using reloadData? – Trip Phillips Nov 25 '15 at 20:45
  • No you should try to add someting like `currentCell.Invited.image = UIImage(named: "checked.png")` in `cellForRowAtIndexPath` method when populating the `UITableView` – Martijn Nov 25 '15 at 20:46
  • Yeah because I did some debugging and the user that isn't supposed to get selected doesn't get added to the allFriends array. Just the image changes for some reason. I will try this out. – Trip Phillips Nov 25 '15 at 20:59