在UITableViewCell中向左或向右滑动以删除没有删除按钮的单元格? [英] Swipe left or right anywhere in a UITableViewCell to delete the cell with no delete button?
问题描述
我希望能够在表格视图单元格中的任何位置向左或向右滑动以删除带有动画的单元格而不显示删除按钮。我怎么能这样做?
I want to be able to swipe left or right anywhere in a table view cell to erase the cell of with animation without showing the delete button. How can I do this?
推荐答案
我没试过并实现过这个,但我会试一试。首先,创建一个自定义UITableViewCell,并让它具有2个可以使用的属性
I have not tried and implemented this, but I'll give it a shot. First, create a custom UITableViewCell, and let it have 2 properties that you can use
- 对正在使用的tableView的引用。
- 用于在tableView中查找单元格的indexPath。(注意,每次删除此更改的所有单元格的单元格时都需要更新)
在您创建自定义单元格的 cellForRowAtIndexPath:
中,设置这些属性。还要向单元格添加UISwipeGestureRecognizer
In your cellForRowAtIndexPath:
, where you create the custom cell, set these properties. Also add a UISwipeGestureRecognizer to the cell
cell.tableView=tableView;
cell.indexPath=indexPath;
UISwipeGestureRecognizer *swipeGestureRecognizer=[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(deleteCell:)];
[cell addGestureRecognizer:swipeGestureRecognizer];
[swipeGestureRecognizer release];
确保手势只接收水平滑动。
Make sure that the gesture only receives horizontal swipes.
-(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
}
这篇关于在UITableViewCell中向左或向右滑动以删除没有删除按钮的单元格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!