滑动以删除不显示删除按钮

时间:2013-10-28 21:25:18

标签: uitableview ios7 swipe

我肯定在这里遗漏了一些东西。这不应该那么难。我正在尝试在iPad上的UITableView中的项目列表上实现基本滑动以删除功能。一切似乎都有效,除了当单元格向左滑动时,没有删除按钮只是空白空间。以下是我在相应功能中的内容。

- (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
    }
}

修改

只是尝试随机的东西我添加了以下代码。当我滑动列表单元格时,我看到我创建的红色删除按钮瞬间出现,然后滑出单元格的右边缘。因此,从iOS的外观来看,editAccessory视图不在单元格的边缘。还在试图找出原因。

- (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];
}

1 个答案:

答案 0 :(得分:0)

我应该在不久前发布这个但我忘记了。显然iOS正在调整菜单ViewController下的幻灯片到全屏,没有任何理由,无需调整列表项的子视图。所以当我刷卡删除时,删除按钮隐藏在父ViewController的主视图下。以下代码行修复了问题。

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