如何用长按手势开始移动细胞?

时间:2012-06-27 08:32:30

标签: iphone

我有一个UITableView,里面有几个UITableViewCells。我想长按一个单元格开始移动它并继续按住它并拖动它移动它。但我不知道如何。

我在桌面视图上添加了一个长按手势,当用户长按时,将tableview的编辑设置为YES:

UILongPressGestureRecognizer *longPressRecognizer = [[UILongPressGestureRecognizer alloc] 
                                                     initWithTarget:self
                                                     action:@selector(handleTableViewLongPress:)];
[_tableView addGestureRecognizer:longPressRecognizer];

- (void)handleTableViewLongPress:(UILongPressGestureRecognizer *)gesture
{
    if (gesture.state != UIGestureRecognizerStateBegan)
        return;
    [_tableView setEditing:YES];
}

tableview可以用两种方法移动单元格:

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
    //update datasource
}

但是使用这些代码用户必须使用两个触摸来移动一个单元格:一个是将tableview设置为编辑状态,另一个是移动单元格。我想要的是允许用户使用一触即可。

有什么建议吗?谢谢!

1 个答案:

答案 0 :(得分:1)

我找到了这个UITableView子类:Move table view

相关问题