添加“滑动以删除”但不是在所有单元格中

时间:2011-08-03 16:43:59

标签: iphone uitableview swipe

我想在我的tableview中添加“滑动删除”的可能性,但我不希望这个表格的最后一个单元格!

我可以检查- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath是否indexPath.row是最后一个,但我想要的是,如果用户在最后一个单元格上滑动,则不会出现任何内容(而在其他单元格中则显示文本“删除“)。

我试过这个

-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.row != ([array count]-1)) {
   return @"delete";
 }
 else {
     NSString *a;
     return a;
  }
}

但当然它不起作用(应用程序崩溃)。 我试过

return @"";

但出现红色按钮(没有文字)!

你有什么建议我? 谢谢!

2 个答案:

答案 0 :(得分:3)

尝试

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([indexPath row] == [tableView numberOfRowsInSection:[indexPath section]] - 1)
    {
        return UITableViewCellEditingStyleNone;
    }

    return UITableViewCellEditingStyleDelete;
}

答案 1 :(得分:3)

自从您返回未初始化的指针后,应用程序崩溃了。但即便如此,你做错了; - )

您希望为所有单元格实现tableView:editingStyleForRowAtIndexPath:并返回UITableViewCellEditingStyleDelete,但最后一个单元格除外。您需要为最后一个单元格返回UITableViewCellEditingStyleNone以防止它被删除。