0

I have implemented code that returns 0 every time. I'm trying to remove a string from a mutable array with the row value selected after hitting a button.

Related code:

- (IBAction)remove:(id)sender {
    NSIndexPath *selectedIndexPath = [_tableView2 indexPathForSelectedRow];
    [names removeObject:[NSString stringWithFormat:@"%i",selectedIndexPath.row]];
    testLabel.text=[NSString stringWithFormat:@"%i",selectedIndexPath.row];
    [_tableView2 reloadData];
}

The test label shows 0 every time the button is pressed, no matter where the tableview is selected.

Let me know if there is other relevant code (like the tableView) that you want me to post.

1
  • please see my update anser.
    – LuisCien
    Commented Jun 11, 2013 at 21:14

2 Answers 2

1

For a UITableView, the selected row concept is only valid while the user is touching the row. For that reason indexPathForSelectedRow is documented as returning “An index path identifying the row and section indexes of the selected row or nil if the index path is invalid.”

My opinion is that you are obtaining a nil result, and later calling the row method in that nil results in the zero that your are seeing as your name.

The solution depends on the rest of your data source implementation, but probably will involve storing the tapped index in didSelectRowAtIndexPath: to later use it in your remove method.

(I supposed that you are not using the allowsMultipleSelectionDuringEditing option nor are you editing the table).

5
  • I thought what I had implemented seemed too easy... Thank you. I'll look into didSelectRowAtIndexPath documentation and get back to you. And yes, you're right in assuming I'm not allowing multiple selections. Commented Jun 11, 2013 at 20:39
  • Unless you set UITableView's property 'allowsSelection' to 'NO' I believe this is not the case. When you select a cell in a table view the cell remains selected until you send the 'deselectRowAtIndexPath:animated:' message to it
    – LuisCien
    Commented Jun 11, 2013 at 20:41
  • You are completely right @LuisCien, but I thought that he either didn’t implement didSelectRowAtIndexPath: (which makes the deselection by default) or he implemeted it in the most usual way (which involves calling the deselect method at the end, in case there is no view controller change).
    – yonosoytu
    Commented Jun 11, 2013 at 21:01
  • What do you mean by editing the table? The selection is being removed by removing an item from it's data source and reloading the table. Is that a problem? Commented Jun 11, 2013 at 21:58
  • The UITableView has an editing mode. It’s mostly useful when you need the table to give access to the items, but also other secondary actions. Look at the Mail app for an idea. You are doing it right by removing the element from the data source and reloading the data.
    – yonosoytu
    Commented Jun 11, 2013 at 22:41
1

Instead of:

[names removeObject:[NSString stringWithFormat:@"%i",selectedIndexPath.row]];

Try:

[name removeObjectAtIndex:selectedIndexPath.row];

Also, instead of using @"%i", try using @"%d" as mentioned here

Hope this helps!

2
  • This is better code than I had, but unfortunately, as I mentioned above, my problem is that the selectedIndexPath.row returns 0 every time. I have made the changes with the same result. Thanks though! Commented Jun 11, 2013 at 20:28
  • Try to NSLog 'selectedIndexPath' when you first obtain it.
    – LuisCien
    Commented Jun 11, 2013 at 20:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.