0
votes

For some reason my UITableView Delegate method didSelectRowAtIndexPath is not getting called until after I select the row. Also, although I set the editing style of my UITableView to UITableViewCellEditingStyleDelete, when I swipe my finger across the tableview it doesn't show the delete button. I have set the delegate and datasource properties of my tableview in storyboard to my viewcontroller, but the delegate methods still aren't getting called properly. The cells still function and will navigate to my other detailview, but I'm just getting some very weird behavior. Here's the code I'm using for my tableview:

#pragma mark - Table View

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
        return [_lists count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    return 44;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"MasterListCell";

    /* Set up list cell */
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    CGRect myImageRect = CGRectMake(0.0f, 0.0f, 15.0f, 15.0f);
    UIImageView *myImage = [[UIImageView alloc] initWithFrame:myImageRect];
    [myImage setImage:[UIImage imageNamed:@"cell-arrow.png"]];

    cell.accessoryView = myImage; //cellArrowNotScaled;
    cell.editingAccessoryType = UITableViewCellEditingStyleDelete;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    /* Define a new List */
    List *list = [_lists objectAtIndex:indexPath.row];
   // cell.selectionStyle = UITableViewCellSelectionStyleNone;

    cell.textLabel.font = [UIFont fontWithName:@"Roboto-Medium" size:15];
    cell.textLabel.text = list.name;



    cell.textLabel.highlightedTextColor = [UIColor blackColor];

    return cell;
}


- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return YES if you want the specified item to be editable.
    return YES;
}

// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:

    (NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        //add code here for when you hit delete
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Are you sure?" message:@"This list will be permanently deleted." delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK",nil];
        [alert show];

    }
}


- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSIndexPath *currentSelectedIndexPath = [tableView indexPathForSelectedRow];
    if (currentSelectedIndexPath != nil)
    {
        [[tableView cellForRowAtIndexPath:currentSelectedIndexPath] setBackgroundColor:[UIColor yellowColor]];
    }

    return indexPath;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"did select row");
    [[tableView cellForRowAtIndexPath:indexPath] setBackgroundColor:[UIColor yellowColor]];
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (cell.isSelected == YES)
    {
        [cell setBackgroundColor:[UIColor yellowColor]];
    }
    else
    {
        [cell setBackgroundColor:[UIColor whiteColor]];
    }
}
1
Are any of the delegate methods being called? Is this a copy-paste of your code? (I ask that because of UITableView didSelectRowAtIndexPath: not being called on first tap.)jscs
Have you declared that the view controller follows the UITableViewDataSource and UITableViewDelegate protocols?Cameron
@JoshCaswel yes to both of you actuallyApollo
You said "method didSelectRowAtIndexPath is not getting called until after I select the row". Are you saying that the call is made, just later than you expected?Firoze Lafeer
You'd be better off implementing custom highlight colors in a cell subclass, not the view controller. Also, it seems like you're interested in highlighting, not selection, so you should be using tableView:didHighlightRowAtIndexPath: / tableView:didUnhighlightRowAtIndexPath: (but again: not a good approach).omz

1 Answers

1
votes

Update answer

From your comment below, I see what you're getting at. You're trying to fake a custom selected background for grouped style (which can't be customized without providing custom images) by turing of selection highlighting and instead setting the unselected background color when the cell is tapped. You can do this in shouldHighlightRowAtIndexPath:

- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.backgroundColor = [UIColor greenColor];
    return YES;
}

This method gets called before didSelectRowAtIndexPath even when selection style is none. You'll need to elaborate on the above solution to set the color back when the cell is supposed to be unhighlighted.

Original answer

didSelectRowAtIndexPath is not getting called until after I select the row

That is by design, hence the past tense "did" in the name.

Also, although I set the editing style of my UITableView to UITableViewCellEditingStyleDelete, when I swipe my finger across the tableview it doesn't show the delete button.

You've got to implement the data source method tableView:commitEditingStyle:forRowAtIndexPath: to have the delete button appear. If you think about it it makes sense. You haven't provided a way for your data source to respond to the edit, so iOS concludes that it shouldn't edit.

I'm saying that when I press down on a cell with UITableViewSelectionStyleBlue it shows up blue. However, when I set it to none and attempt to change the background color in didSelectRowAtIndexPath it doesn't change until after I have lifted my finger up off the cell. I want the background color of the cell to change when I put my finger down without lifting it

What are you ultimately trying to accomplish? It sounds like you want to do a custom highlight color. The way to do that is to replace the cell's selectedBackgroundView with your own view and set that view's background color:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //...
    cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.bounds];
    cell.selectedBackgroundView.backgroundColor = [UIColor greenColor];
    //...
}

If that's not what you're going for, please clarify and I'll update my answer.