删除uitableview中的行
我有一个应用程序,如果用户input数据的行将更新与该数据
我可以使用一个单一的button说'删除',当单击时,将删除所有的表中的行一次。
做一个button,并在button的操作方法
-(IBAction)deleteRows { [array removeAllObjects]; [tableview reloadData]; }
是的,你可以这么做。 首先从您的数据源中删除所有数据,然后重新加载您的表。 例如。 –
[yourArrayDataSource removeAllObjects]; [yourTable reloadData];
要animation删除行 – 在IBAction
方法中执行此操作并将其链接到您的UIButton
。 只要你按下button,你将有一个平滑的令人敬畏的animation,使所有的行淡出。
-(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]; }
有各种animation,你可以在这里使用 –
UITableViewRowAnimationBottom UITableViewRowAnimationFade UITableViewRowAnimationMiddle UITableViewRowAnimationNone UITableViewRowAnimationRight UITableViewRowAnimationTop
Srikar的回答让我走上了正确的轨道,但创build了大量额外的单个项目数组,并且调用了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]; }