【问题标题】:how to begin moving cell with long press gesture?如何用长按手势开始移动单元格?
【发布时间】:2012-06-27 08:32:29
【问题描述】:

我有一个 UITableView,里面有几个 UITableViewCells。我想长按一个单元格以开始移动它并按住它并拖动以移动它。但我不知道怎么做。

我在表格视图上添加了一个长按手势,当用户长按时,将表格视图的编辑设置为是:

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 设置为编辑状态,另一个是移动单元格。我想要的是允许用户使用一键来做到这一点。

有什么建议吗?谢谢!

【问题讨论】:

  • 是的,我有。但长按后,我需要再次触摸来移动单元格。我想要的是用户只能使用一次触摸。

标签: iphone


【解决方案1】:

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

【讨论】:

  • 太棒了!这正是我想要的
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-15
  • 1970-01-01
  • 2019-06-11
相关资源
最近更新 更多