删除 UIView 中 TableView 的记录

Posted

技术标签:

【中文标题】删除 UIView 中 TableView 的记录【英文标题】:Delete records of a TableView inside a UIView 【发布时间】:2011-12-24 11:30:12 【问题描述】:

我正在尝试在 Storyboard 的 UIView 中实现 TableView。 这就是我所取得的成就:

现在我希望编辑按钮(如果按下)准备删除表行。 我将编辑按钮放在栏上,如下所示:

//add edit button
[buttons addObject:self.editButtonItem];

此外,我已将 tableview 委托和数据源设置为主视图并触发事件。但是如何告诉编辑按钮表格的行必须是可删除的?

提前谢谢你, 亚萨

【问题讨论】:

【参考方案1】:

有这个委托的tabelView方法:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                     withRowAnimation:UITableViewRowAnimationFade];
    //some other bookkeeping stuff

添加了按下时编辑按钮的代码。将“toggleEdit”重命名为适合您需要的任何名称。

- (IBAction)toggleEdit:(id)sender   //this method should get called when Edit is pressed

    [self.tableView setEditing:!self.tableView.editing animated:YES];


//The if ... else part is optional.  You might need to make some change to fit your's.
//    if (self.tableView.editing)
//        [self.navigationItem.rightBarButtonItem setTitle:@"Done"];
//    else
//        [self.navigationItem.rightBarButtonItem setTitle:@"Delete"];

【讨论】:

感谢您的回答。我已经实现了委托方法。我的问题是我不知道如何将“编辑”按钮引用到我的视图中的表格...

以上是关于删除 UIView 中 TableView 的记录的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Swift 中使用 MagicalRecord CoreData 删除 tableview 中的记录

如何保持 Tableview 滚动以及如何在 Swift3 中删除原型单元和 tableview 之间的空白空间

iOS 如何设置tableview列表的cell上的的设置删除键 置顶按钮 未读信息按钮

TableView 中 UIView 的 CAGradientLayer 未调整为自动布局

删除 CoreData 对象,然后调用 [tableView reloadData] 会导致 SIGABRT

UIView 动画打破 TableView