Search code examples
uitableviewios7swipe

swipe to delete not showing the delete button


I'm definitely missing something here. This should not be that hard. I am trying to implement the basic swipe to delete function on a list of items in a UITableView on an iPad. Everything seems to work except when the cell slides to the left there is NO delete button just empty white space. Below is what I have in the appropriate functions.

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.state == AMListMenu && [indexPath row] > 1)
    {
        return YES;
    }

    return NO;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.state == AMListMenu && [indexPath row] > 1)
    {
        return UITableViewCellEditingStyleDelete;
    }

    return UITableViewCellEditingStyleNone;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }
}

EDIT:

Just trying random things I added the following code. When I swipe the list cell I see the red delete button I created appear for a split second and then slide off the right edge of the cell. So, from the looks of things iOS is putting the editAccessory view off the edge of the cell. Still trying to figure out why.

- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
    MainMenuCell *cell = (MainMenuCell *)[tableView cellForRowAtIndexPath:indexPath];

    UIButton* btn = [[UIButton alloc]init];
    [btn setTitle:@"DELETE" forState:UIControlStateNormal];
    [btn setTitleColor:AMColorWhite forState:UIControlStateNormal];
    [btn setBackgroundColor:AMColorTradeMarkRed];
    [btn setFrame:CGRectMake(242, 0, 90, cell.frame.size.height)   ];

    [cell setEditingAccessoryView:btn];
    [cell.contentView addSubview:btn];
}

Solution

  • I should of posted this a while ago but I forgot. Apparently iOS was resizing the slide under menu ViewController to full screen for no good reason without resizing the subviews of the list items. So when I swiped to delete the delete button was hidden under the main view of the parent ViewController. The following line of code fixed the problem.

    [self.mainMenu.view setFrame:CGRectMake(0, 0, MENUWIDTH, self.view.frame.size.height)];