您的位置:首页 > 产品设计 > UI/UE

向左或向右滑动在任何地方一个UITableViewCell删除的单元格没有删除按钮?

2014-07-25 16:10 393 查看
1. 我还没有试过这,但我给它一个镜头。首先,创建一个自定义的UITableViewCell,并让它有2个属性,您一提到的tableView这是英寸 一个indexPath来查找的tableView细胞(注意,这需要更新您删除单元格的所有单元格,其中这改变)在你的
cellForRowAtIndexPath:
,在那里你创建自定义单元格,设置这些属性。还添加了UISwipeGestureRecognizer到细胞
cell.tableView=tableView;
cell.indexPath=indexPath;
UISwipeGestureRecognizer *swipeGestureRecognizer=[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@"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
}


2. 发布者@MadhavanRP该解决方案的工作原理,但它比它需要。你可以采取一个更简单的方法,并创建一个手势识别来处理发生在表中的所有挥笔,然后拿到刷卡的位置,以确定哪些单元格刷卡。 要设置手势识别:
- (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必须在手势识别的委托。

3.
- (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;

仅此而已。

4. 对于u必须添加此项目中的代码。
// 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
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐