52

I have a UITableViewController where the user should be able to edit the items. In order to enable editing I use this :

self.navigationItem.rightBarButtonItem = self.editButtonItem;

For everyone who does not know where self.editButtonItem comes from, it is predefined by the SDK.

So, now when I press on any item, this method is called :

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 

but as soon as I hit the edit button and while editing is active, this method does not seemed to be called.

Any idea what I missing ?

This is the rest of the code that is related to editing :

// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath 
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    return UITableViewCellEditingStyleNone;
}


- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath 
{
    return NO;
}

Thanks for the help.

mcb

Benjohn
  • 13,228
  • 9
  • 65
  • 127
elementsense
  • 1,540
  • 3
  • 15
  • 21

5 Answers5

143

You gotta set the allowsSelectionDuringEditing property of UITableView to true.

Reference: UITableView - allowsSelectionDuringEditing

Pang
  • 9,564
  • 146
  • 81
  • 122
Daniel
  • 22,363
  • 9
  • 64
  • 71
0

I solved this problem other way. My Edit button was on my own Navigation bar and that was the reason it wasn't called. After I've embedded VC inside Navigation Controller, and put new Edit button inside its Navigation Item, Edit button started to get called.

RealNmae
  • 630
  • 9
  • 20
0

Do you set the self.editing = YES when the edit button is tapped?

When in editing mode this method is called, instead of didSelectRow when you tap a row:

tableView:commitEditingStyle:forRowAtIndexPath:.
RickiG
  • 11,380
  • 13
  • 81
  • 120
  • im not sure if your claim is true, commitediting gets caleld when u actually delete or edit the cell...read my answer, thats the way to get didSelectRow call backs whil editing – Daniel Jun 15 '10 at 18:02
  • Hi, you are of course completely right! I wasn't paying attention :/ sorry. – RickiG Jun 15 '10 at 21:15
0

Short answer (Swift lang):

@IBOutlet private weak var tableView: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()

    tableView.allowsSelectionDuringEditing = true // Magic happens here
}
nurtugan
  • 79
  • 2
  • 2
-2

but as soon as I hit the edit button and while editing is active, this method does not seemed to be called.

Any idea what I missing ?

Yes. You are expecting tableView:didSelectRowAtIndexPath: to be called when a button in your nav bar is tapped. This will never happen. That method is only called when a row is selected.

When the edit button is tapped, you will want to put your table into editing mode. In your view controller:

- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated]; // must be called first according to Apple docs

    [self.tableView setEditing:editing animated:animated];
}

Then you handle editing events by adding a tableView:commitEditingStyle:forRowAtIndexPath: method.

Shaggy Frog
  • 27,575
  • 16
  • 91
  • 128
  • 1
    He is asking why isnt didSelectRow is not called while he is editing, he is not expecting it to occur when he taps edit – Daniel Jun 15 '10 at 18:07
  • @Daniel If you look closely, you'll see that I answered the question as it was posed. – Shaggy Frog Jun 15 '10 at 19:22