向左或向右滑动UITableViewCell中的任何位置以删除没有删除button的单元格?
我希望能够在表格视图单元格中的任意位置向左或向右滑动,以删除带有animation的单元格,而不显示删除button。 我怎样才能做到这一点?
我没有尝试和实施这个,但我会给它一个镜头。 首先,创build一个自定义的UITableViewCell,让它有2个属性,你可以使用
- 它正在使用的tableView的引用。
- 在indexView中查找单元格的索引path(注意,每次删除所有单元格的单元格时,都需要更新)
在您的cellForRowAtIndexPath:
您创build自定义单元格,设置这些属性。 还要添加一个UISwipeGestureRecognizer到单元格
cell.tableView=tableView; cell.indexPath=indexPath; UISwipeGestureRecognizer *swipeGestureRecognizer=[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(deleteCell:)]; [cell addGestureRecognizer:swipeGestureRecognizer]; [swipeGestureRecognizer release];
确保手势只接收水平滑动。
-(BOOL) gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch { if([[gestureRecognizer view] isKindOfClass:[UITableViewCell class]]&& ((UISwipeGestureRecognizer*)gestureRecognizer.direction==UISwipeGestureRecognizerDirectionLeft ||(UISwipeGestureRecognizer*)gestureRecognizer.direction==UISwipeGestureRecognizerDirectionRight)) return YES; }
在你的deleteCell:
-(void) deleteCell:(UIGestureRecognizer*)gestureRec { UIGestureRecognizer *swipeGestureRecognizer=(UISwipeGestureRecognizer*)gestureRec; CustomCell *cell=[swipeGestureRecognizer view]; UITableView *tableView=cell.tableView; NSIndexPath *indexPath=cell.indexPath; //you can now use these two to perform delete operation }
@MadhavanRP发布的解决scheme的工作,但它比它需要更复杂。 您可以采取更简单的方法,并创build一个手势识别器来处理表中出现的所有滑动,然后获取滑动的位置以确定用户滑动的单元格。
设置手势识别器:
- (void)setUpLeftSwipe { UISwipeGestureRecognizer *recognizer; recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeLeft:)]; [recognizer setDirection:UISwipeGestureRecognizerDirectionLeft]; [self.tableView addGestureRecognizer:recognizer]; recognizer.delegate = self; }
在viewDidLoad
调用这个方法
要处理滑动:
- (void)swipeLeft:(UISwipeGestureRecognizer *)gestureRecognizer { CGPoint location = [gestureRecognizer locationInView:self.tableView]; NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location]; ... do something with cell now that i have the indexpath, maybe save the world? ... }
注意:你的vc必须实现手势识别器委托。
实行
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath; - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath;
这两个方法,如果UITableViewCellEditingStyle是UITableViewCellEditingStyleDelete,那么做一下擦除你的单元格
- (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;
就这样。
为此,你必须在你的项目中添加这个代码。
// Override to support conditional editing of the table view. - (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 } }
Else mor info你可以通过这个链接进入链接描述在这里