xcode 删除uitableview中的行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8006113/
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
Deleting rows in uitableview
提问by Chandu
I am Having an application where, if the user enters data the rows will be updated with that data
我有一个应用程序,如果用户输入数据,行将用该数据更新
Can i use One Single Button say 'delete' which when clicked will delete all the rows in the tableview at once.?
我可以使用单个按钮说“删除”,单击该按钮将立即删除 tableview 中的所有行。?
回答by Srikar Appalaraju
Yes you can do that. First remove all data from your data source, then reload your table. For ex. -
是的,你可以这样做。首先从数据源中删除所有数据,然后重新加载表。例如。——
[yourArrayDataSource removeAllObjects];
[yourTable reloadData];
To animate the deletion of rows - do this in an IBAction
method & link it to your UIButton
. As soon as you press the button you will have a smooth awesome animation making all your rows fade out.
要动画删除行 - 在IBAction
方法中执行此操作并将其链接到您的UIButton
. 只要你按下按钮,你就会有一个流畅的很棒的动画,让你所有的行都淡出。
-(IBAction)deleteRows
{
[yourTable beginUpdates];
for(int i=0; i<[yourArrayDataSource count]; i++)
{
indexPath = [NSIndexPath indexPathForRow:i inSection:0];
[self.searchResTable deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
[yourTable endUpdates];
}
There are various animations that you can use here-
您可以在这里使用各种动画-
UITableViewRowAnimationBottom
UITableViewRowAnimationFade
UITableViewRowAnimationMiddle
UITableViewRowAnimationNone
UITableViewRowAnimationRight
UITableViewRowAnimationTop
回答by Tendulkar
make a button and in the button action method
制作一个按钮并在按钮动作方法中
-(IBAction)deleteRows
{
[array removeAllObjects];
[tableview reloadData];
}
回答by averydev
Srikar's answer put me on the right track, but creates a lot of extra single item arrays, and calls deleteRowsAtIndexPaths far more than is needed.
Srikar 的回答使我走上了正确的轨道,但创建了许多额外的单项数组,并且调用 deleteRowsAtIndexPaths 的次数远远超过所需。
-(void)clearTable
{
NSMutableArray *indexPaths = [NSMutableArray array];
for(int i=0; i<[self.myArray count]; i++)
{
NSIndexPath *anIndexPath = [NSIndexPath indexPathForRow:i inSection:0];
[indexPaths addObject:anIndexPath];
}
[self.myTableView beginUpdates];
[self.myTableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
self.myArray = [NSArray array];
[self.myTableView endUpdates];
}