10

Feel like I'm going a bit nutty here. I have a detail view with a few stand-alone UITextFields, a few UITextFields in UITAbleViewCells, and one single UITableViewCell that will be used to hold notes, if there are any. I only want this cell selectable when I am in edit mode. When I am not in edit mode, I do not want to be able to select it. Selecting the cell (while in edit mode) will fire a method that will init a new view. I know this is very easy, but I am missing something somewhere.

Here are the current selection methods I am using:

-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (!self.editing) {
        NSLog(@"Returning nil, not in edit mode");
        return nil;
    }
    NSLog(@"Cell will be selected, not in edit mode");
    if (indexPath.section == 0) {
        NSLog(@"Comments cell will be selected");
        return indexPath;
    }
    return nil;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (!self.editing) {
        NSLog(@"Not in edit mode. Should not have made it this far.");
        return;
    }

    if (indexPath.section == 0)
        [self pushCommentsView];
    else
        return;
}

My problem is really 2 fold;

1) Even when I'm not in edit mode, and I know I am returning nil (due to the NSLog message), I can still select the row (it flashes blue). From my understanding of the willSelectRowAtIndexPath method, this shouldn't be happening. Maybe I am wrong about this?

2) When I enter edit mode, I can't select anything at all. the willSelectRowAtIndexPath method never fires, and neither does the didSelectRowAtIndexPath. The only thing I am doing in the setEditing method, is hiding the back button while editing, and assigning firstResponder to the top textField to get the keyboard to pop up. I thought maybe the first responder was getting in the way of the click (which would be dumb), but even with that commented out, I cannot perform the cell selection during editing.

Gordon Fontenot
  • 1,370
  • 3
  • 17
  • 38

2 Answers2

16

Good lord I am an idiot. I never added these lines:

self.tableView.allowsSelection = NO; // Keeps cells from being selectable while not editing. No more blue flash.
self.tableView.allowsSelectionDuringEditing = YES; // Allows cells to be selectable during edit mode.

Sorry for the garbage question.

Gordon Fontenot
  • 1,370
  • 3
  • 17
  • 38
4

The documentation notes that tableView:willSelectRowAtIndexPath: isn't called when in editing mode. In addition, the blue flash will happen even if you cancel the selection. From the documentation:

This method is not called until users touch a row and then lift their finger; the row isn't selected until then, although it is highlighted on touch-down. You can use UITableViewCellSelectionStyleNone to disable the appearance of the cell highlight on touch-down. This method isn’t called when the editing property of the table is set to YES (that is, the table view is in editing mode).

MrHen
  • 2,420
  • 2
  • 25
  • 39
  • 1
    Thanks for the answer. Got me to double check the docs, which made me realize my dumb mistake. – Gordon Fontenot May 05 '10 at 18:38
  • Just want to clarify this answer by quoting the documentation in full: "...This method isn’t called when the table view is in editing mode (that is, the editing property of the table view is set to YES) **unless the table view allows selection during editing (that is, the allowsSelectionDuringEditing property of the table view is set to YES).**" – Pat Jul 05 '13 at 21:18