objective-c 如何在 iPhone 中删除表格的一行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1168593/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to delete a row of a Table in iPhone
提问by rkb
I have a UITableView and I want to provide the functionality to user to delete the row when he slips or flicks his finger on the row. I know the editing style which provides a circular red button with -ve sign on it. But How to implement the flicking style. I saw many applications using it, so does apple provide any inbuilt delegate for it or we need to write our own controller for it.
我有一个 UITableView,我想为用户提供在他在行上滑动或轻弹手指时删除行的功能。我知道提供带有 -ve 标志的圆形红色按钮的编辑风格。但是如何实现轻弹风格。我看到很多应用程序都在使用它,苹果是否为它提供了任何内置的委托,或者我们需要为它编写我们自己的控制器。
回答by paulthenerd
In order to get the swipe affect you need to implement the table view delegate
为了获得滑动效果,您需要实现表视图委托
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
method, this will provide access to the swipe interaction for deletion. I typically provide an edit interaction as well for tableviews where deletion is possible since the swipe interaction tends to be a little bit hidden from users.
方法,这将提供对用于删除的滑动交互的访问。我通常也为可以删除的 tableview 提供编辑交互,因为滑动交互往往对用户有点隐藏。
As an example:
举个例子:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView beginUpdates];
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Do whatever data deletion you need to do...
// Delete the row from the data source
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationTop];
}
[tableView endUpdates];
}
Hope that helps.
希望有帮助。
回答by TahoeWolverine
I'm almost positive that there is a sample application that does this. A more concrete answer will come soon.
我几乎肯定有一个示例应用程序可以做到这一点。很快就会有更具体的答案。
UPDATE: iPhoneCoreDataRecipesgives probably exactly what you're looking for.
更新:iPhoneCoreDataRecipes可能正是您正在寻找的。
On topic, here is one of the sweetest provided methods:
关于主题,这是提供的最甜蜜的方法之一:
// If I want to delete the next 3 cells after the one you click
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSMutableArray* indexPaths = [NSMutableArray array];
for (int i = indexPath.row + 3; i < indexPath.row + 3; i++)
{
[indexPaths addObject:[NSIndexPath indexPathForRow:i inSection:0]];
}
[tableView beginUpdates];
[tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
[tableView endUpdates];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
I think that there is an easier way to accomplish what you want, but then again this is pretty easy. The only difficulty might be deleting yourself...just watch out for segfaults.
我认为有一种更简单的方法来完成你想要的,但是这又很容易。唯一的困难可能是删除自己……只是要注意段错误。

