如何在自定义的UITableviewCell中检测滑动到删除手势

如何在自定义的UITableviewCell中检测滑动到删除手势

本文介绍了如何在自定义的UITableviewCell中检测滑动到删除手势?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经定制了一个UITableViewCell,我想实现轻扫删除。但我不想要默认的删除按钮。相反,我想做一些不同的事情。实现这个最简单的方法是什么?当用户滑动删除单元格时,是否有一些方法被调用?我可以阻止默认删除按钮出现吗?

I have customized a UITableViewCell and I want to implement "swipe to delete". But I don't want the default delete button. Instead, I want to do something different. What would be the easiest way to implement this? Are there some methods which get called when the user swipes to delete a cell? Can I prevent then the default delete button from appearing?

现在我认为我必须实现自己的逻辑以避免默认删除按钮并缩小滑动中发生的动画在UITableViewCell的默认实现中删除。

Right now I think I must implement my own logic to avoid the default delete button and shrink animations which happen in swipe to delete in the default implementation of UITableViewCell.

也许我必须使用UIGestureRecognizer?

Maybe I have to use a UIGestureRecognizer?

推荐答案

如果你想要做一些完全不同的事情,将UISwipeGestureRecognizer添加到每个tableview单元格。

If you want to do something completely different, add a UISwipeGestureRecognizer to each tableview cell.

// 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];
        //..
    }
}

这篇关于如何在自定义的UITableviewCell中检测滑动到删除手势?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 06:35