【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>>
如何在UITableViewCell
上滑动时显示删除按钮? 永远不会引发该事件,并且永远不会出现删除按钮。
#1楼
注意:我没有足够的声誉在Kurbz的答案中发表评论。
Kurbz的答案是对的。 但对我来说它从来没有奏效。
经过一些调查,我意识到在不编辑表格视图时会发生滑动到删除。 。
我从未见过如此明确说明。 除非我弄错了,否则我没有找到任何其他办法让它发挥作用。
编辑时,将显示删除和/或重新排序控件。
#2楼
删除tableview的单元格时,还必须删除索引x处的数组对象。
我认为您可以使用滑动手势将其删除。 表视图将调用Delegate:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
//add code here for when you hit delete
[dataSourceArray removeObjectAtIndex:indexPath.row];
}
}
删除对象后。 您必须重新加载tableview使用。 在代码中添加以下行:
[tableView reloadData];
之后,您已成功删除该行。 当您重新加载视图或向DataSource添加数据时,对象将不再存在。
对于所有其他人来说,Kurbz的答案是正确的。
我只想提醒您,如果要从DataSource数组中删除对象,则委托函数是不够的。
我希望我能帮助你。
#3楼
我有一个问题,我刚刚设法解决,所以我分享它,因为它可以帮助某人。
我有一个UITableView并添加了显示的方法以启用滑动删除:
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
// Return YES if you want the specified item to be editable.
return YES;
}
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
//add code here for when you hit delete
}
}
我正在进行一项更新,允许我将表格置于编辑模式并启用多选。 为此,我添加了Apple的TableMultiSelect示例中的代码。 一旦我开始工作,我发现我的刷卡删除功能已停止工作。
事实证明,向viewDidLoad添加以下行是问题:
self.tableView.allowsMultipleSelectionDuringEditing = YES;
使用此行,多选将起作用,但删除的滑动不会。 没有这条线就是另一种方式。
修复:
将以下方法添加到viewController:
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
self.tableView.allowsMultipleSelectionDuringEditing = editing;
[super setEditing:editing animated:animated];
}
然后在您将表格置于编辑模式的方法中(例如按下按钮),您应该使用:
[self setEditing:YES animated:YES];
代替:
[self.tableView setEditing:YES animated:YES];
这意味着仅当表处于编辑模式时才启用多选。
#4楼
此代码显示了如何实现删除。
#pragma mark - UITableViewDataSource
// Swipe to delete.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
[_chats removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
}
(可选)在初始化覆盖中,添加以下行以显示“编辑”按钮项:
self.navigationItem.leftBarButtonItem = self.editButtonItem;
#5楼
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
//add code here for when you hit delete
[dataSourceArray removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
}
来源:oschina
链接:https://my.oschina.net/u/3797416/blog/3155558