Properly setting up willSelectRowAtIndexPath and didSelectRowAtIndexPath to send cell selections

Gordon Fontenot picture Gordon Fontenot · May 5, 2010 · Viewed 14k times · Source

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.

Answer

Gordon Fontenot picture Gordon Fontenot · May 5, 2010

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.