问题描述
|
我已经自定义了UITableViewCell,并且我想实现“擦除以删除”。但是我不想要默认的删除按钮。相反,我想做些不同的事情。实现此目的的最简单方法是什么?当用户滑动删除单元格时,是否有一些方法会被调用?是否可以阻止默认的删除按钮出现?
现在,我认为我必须实现自己的逻辑,以避免默认的删除按钮和收缩动画,这些动画在UITableViewCell的默认实现中通过滑动进行删除。
也许我必须使用UIGestureRecognizer?
解决方法
如果要执行完全不同的操作,请向每个tableview单元格添加一个UISwipeGestureRecognizer。
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @\"Cell\";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell.
UISwipeGestureRecognizer* sgr = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(cellSwiped:)];
[sgr setDirection:UISwipeGestureRecognizerDirectionRight];
[cell addGestureRecognizer:sgr];
[sgr release];
cell.textLabel.text = [NSString stringWithFormat:@\"Cell %d\",indexPath.row];
// ...
return cell;
}
- (void)cellSwiped:(UIGestureRecognizer *)gestureRecognizer {
if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {
UITableViewCell *cell = (UITableViewCell *)gestureRecognizer.view;
NSIndexPath* indexPath = [self.tableView indexPathForCell:cell];
//..
}
}
, 这是可用于避免使用“删除按钮”的两种方法:
- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath
和
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath