【问题标题】:Delete a tableview cell by tapping the red minus icon in iOS通过点击 iOS 中的红色减号图标来删除 tableview 单元格
【发布时间】:2016-01-25 09:18:21
【问题描述】:

当用户单击红色减号图标时,我想立即删除 tableview 单元格,而无需确认按下红色“删除”按钮。 步骤 1. 点击编辑按钮 > 出现红色减号图标 > 点击减号图标 >> 行已删除而不显示红色“删除”图标。

【问题讨论】:

  • 然后向左滑动实现删除。
  • 我想使用确认删除作为点击红色减号图标。所以我需要按照上面提到的确切步骤。
  • 您的问题与您的问题相矛盾:当用户单击红色减号图标时,我想在不确认按下红色“删除”按钮的情况下立即删除 tableview 单元格。。跨度>
  • 我只想通过点击减号图标而不是默认的“删除”按钮来删除单元格。对不起,如果我让你感到困惑..

标签: ios objective-c uitableview


【解决方案1】:

这可以完成您想要的工作。

- (nullable NSString *)tableView:(UITableView *)tableViews titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath 
{
[arrOutputRecords removeObjectAtIndex:indexPath.row];

[tableViews deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                  withRowAnimation:YES];

[_tableView reloadData];

return @"";
}

【讨论】:

  • 可能你误会了..我不想显示删除按钮,直接点击红色减号,项目删除,不需要按删除按钮。
  • 不要在insert/deleteRowsAtIndexPaths之后调用reloadData。插入/删除方法负责更新表。
【解决方案2】:

在你的类中添加这些方法:

override func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle {
    if (self.tableView.editing) {
            return UITableViewCellEditingStyle.Delete;
    }

    return UITableViewCellEditingStyle.None;
}

func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
    if (editingStyle == UITableViewCellEditingStyle.Delete) {

let alertController = UIAlertController(title: "Alert", message: "Are you sure?", preferredStyle:UIAlertControllerStyle.Alert)

 alertController.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default)
   { action -> Void in
     arr.removeAtIndex(indexPath.row)
     tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
   })
   self.presentViewController(alertController, animated: true, completion: nil)

        }
    }

【讨论】:

  • 添加了这个方法。但仍然不起作用。- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { return YES; }
  • 你必须从某个地方打电话给tableView.setEditing = true
  • 所以一旦它从表中删除项目就写tableView.setEditing = false
  • 可能是你误会了..我不想显示删除按钮,只是点击红色减号图标,项目已删除,无需按删除按钮。 tableView.setEditing = false 不会满足我的要求,也没有。
  • 哦,好的。是的,我误解了。我会挖掘你想要的东西
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-06
  • 2018-10-10
  • 1970-01-01
  • 2016-11-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多