我希望能够在表格 View 单元格中的任意位置向左或向右滑动,以在不显示删除按钮的情况下删除带动画的单元格。我该怎么做?
最佳答案
我还没有尝试和实现这个,但我会试一试。首先,创建一个自定义的 UITableViewCell,并让它有 2 个你可以使用的属性
- 对正在使用它的 tableView 的引用。
- 用于在 tableView 中查找单元格的 indexPath。(请注意,每次您删除一个单元格时,都需要更新此单元格的所有单元格)
在您创建自定义单元格的 cellForRowAtIndexPath:
中,设置这些属性。同时向单元格添加一个 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
}
关于objective-c - 在 UITableViewCell 中的任意位置向左或向右滑动以删除没有删除按钮的单元格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9060032/