在部分之间移动uitableviewcells时添加长按手势

时间:2015-03-05 21:28:01

标签: objective-c uitableview uigesturerecognizer long-press

继我之前的问题之后 moveRowAtIndexPath - Moving cells between sections

我已经能够在传统的moveRowAtIndexPath之间移动单元格,即:将Mark从Sales转移到Marketing。

然而现在我想更进一步,在tableview上添加longpress手势,让我可以在单元格上使用longpress移动单元格。

作为参考,我的表格是Employees在不同Departments工作的列表;模型是自定义的,但只是为名称保存简单的字符串。

我的行设置是这样的;

-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
    return [_objects count];
}

-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    Department *department = [_objects objectAtIndex:section];
    return [department.employees count];
}

我当前的moveRowAtIndexPath是:

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
    if (fromIndexPath != toIndexPath ) {

        Department *departmentFrom = [_objects objectAtIndex:fromIndexPath.section];
        Department *departmentTo = [_objects objectAtIndex:toIndexPath.section];

        Employee *employee = [departmentFrom.employees objectAtIndex:fromIndexPath.row];

        [departmentFrom.employees removeObjectAtIndex:fromIndexPath.row];
        [departmentTo.employees insertObject:employee atIndex:toIndexPath.row];
        [tableView reloadData];
    }
}

所以现在我在viewDidLoad,ala;

期间向表中添加了longpress
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGestureRecognized:)];
[self.tableView addGestureRecognizer:longPress];

然后终于拥有了我的longpress代码;

- (IBAction)longPressGestureRecognized:(id)sender {

    UILongPressGestureRecognizer *longPress = (UILongPressGestureRecognizer *)sender;
    UIGestureRecognizerState state = longPress.state;

    CGPoint location = [longPress locationInView:self.tableView];
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location];

    static UIView       *snapshot = nil;        ///< A snapshot of the row user is moving.
    static NSIndexPath  *sourceIndexPath = nil; ///< Initial index path, where gesture begins.

    switch (state) {
        case UIGestureRecognizerStateBegan: {
            if (indexPath) {
                sourceIndexPath = indexPath;

                UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];

                // Take a snapshot of the selected row using helper method.
                snapshot = [self customSnapshoFromView:cell];

                // Add the snapshot as subview, centered at cell's center...
                __block CGPoint center = cell.center;
                snapshot.center = center;
                snapshot.alpha = 0.0;
                [self.tableView addSubview:snapshot];
                [UIView animateWithDuration:0.25 animations:^{

                    // Offset for gesture location.
                    center.y = location.y;
                    snapshot.center = center;
                    snapshot.transform = CGAffineTransformMakeScale(1.05, 1.05);
                    snapshot.alpha = 0.98;
                    cell.alpha = 0.0;

                } completion:^(BOOL finished) {

                    cell.hidden = YES;

                }];
            }
            break;
        }

        case UIGestureRecognizerStateChanged: {
            CGPoint center = snapshot.center;
            center.y = location.y;
            snapshot.center = center;

            // Is destination valid and is it different from source?
            if (indexPath && ![indexPath isEqual:sourceIndexPath]) {

                // ... update data source.

                // ... move the rows.
                [self.tableView moveRowAtIndexPath:sourceIndexPath toIndexPath:indexPath];

                // ... and update source so it is in sync with UI changes.
                sourceIndexPath = indexPath;
            }
            break;
        }

        default: {
            // Clean up.
            UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:sourceIndexPath];
            cell.hidden = NO;
            cell.alpha = 0.0;

            [UIView animateWithDuration:0.25 animations:^{

                snapshot.center = cell.center;
                snapshot.transform = CGAffineTransformIdentity;
                snapshot.alpha = 0.0;
                cell.alpha = 1.0;

            } completion:^(BOOL finished) {

                sourceIndexPath = nil;
                [snapshot removeFromSuperview];
                snapshot = nil;

            }];

            break;
        }
    }
}

#pragma mark - Helper methods

/** @brief Returns a customized snapshot of a given view. */
- (UIView *)customSnapshoFromView:(UIView *)inputView {

    // Make an image from the input view.
    UIGraphicsBeginImageContextWithOptions(inputView.bounds.size, NO, 0);
    [inputView.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    // Create an image view.
    UIView *snapshot = [[UIImageView alloc] initWithImage:image];
    snapshot.layer.masksToBounds = NO;
    snapshot.layer.cornerRadius = 0.0;
    snapshot.layer.shadowOffset = CGSizeMake(-5.0, 0.0);
    snapshot.layer.shadowRadius = 5.0;
    snapshot.layer.shadowOpacity = 0.4;

    return snapshot;
}

可悲的是,它在试图移动细胞的UIGestureRecognizerStateChanged崩溃了;它与移动后/之前的数组不一致有关。

另外,我不确定我做得对吗?这种模式是否会在基于部门的部门和员工列表中发挥作用?

无论如何,我的查询是:如何将longpress手势添加到由部分组成的tableview单元格中,或者..我希望使用长按将Tom从Sales转移到Marketing。

鉴于上述代码,这可能吗?

编辑;

我得到的崩溃是:

  

&#39; NSInternalInconsistencyException&#39;,原因:&#39;无效更新:无效   第0节中的行数。包含在中的行数   更新后的现有部分(3)必须等于数量   更新前的该部分中包含的行(3),加号或减号   从该部分插入或删除的行数(插入0,   删除0)加上或减去移入或移出的行数   该部分(0移入,1移出)。&#39;   ***首先抛出呼叫st


进一步更新:@ 2016年1月14日

原来接受的答案仍然让我崩溃。原因是假设正在进行交换;如果不是这样的话。

我通过重复删除项目然后重新添加项目来解决问题。

这张贴在下面;

 case UIGestureRecognizerStateChanged: {
            CGPoint center = snapshot.center;
            center.y = location.y;
            center.x = location.x;
            snapshot.center = center;

            // Is destination valid and is it different from source?
            if (indexPath && ![indexPath isEqual:sourceIndexPath])
            {
                Department *departmentFrom = [_objects objectAtIndex:sourceIndexPath.section];
                Department *departmentTo = [_objects objectAtIndex:indexPath.section];

                [self.tableView beginUpdates];

                Employee *employee = [departmentFrom.employees objectAtIndex:sourceIndexPath.row];
                [departmentFrom.employees removeObjectAtIndex:sourceIndexPath.row];
                [departmentTo.employees insertObject:employee atIndex:indexPath.row];

                [self.tableView moveRowAtIndexPath:sourceIndexPath toIndexPath:indexPath];

                [self.tableView endUpdates];

                // ... and update source so it is in sync with UI changes.
                sourceIndexPath = indexPath;
            }
            break;
        }

我能看到的唯一问题是tableView:moveRowAtIndexPath和这段代码重复相同的代码。

但我可以稍后清理它。

由于

1 个答案:

答案 0 :(得分:2)

case UIGestureRecognizerStateChanged: {
        CGPoint center = snapshot.center;
        center.y = location.y;
        center.x = location.x;
        snapshot.center = center;

        // Is destination valid and is it different from source?
        if (indexPath && ![indexPath isEqual:sourceIndexPath]) {

            [self.stackTableView beginUpdates];
            // ... update data source.
            NSLog(@"exchanging");
            [self.stackObjectArray exchangeObjectAtIndex:indexPath.section withObjectAtIndex:sourceIndexPath.section];

            // ... move the rows.
            NSLog(@"moving the rows");
            [self.stackTableView moveRowAtIndexPath:indexPath toIndexPath:sourceIndexPath];
            [self.stackTableView moveRowAtIndexPath:sourceIndexPath toIndexPath:indexPath];

            NSLog(@"ending updates");
            [self.stackTableView endUpdates];

            // ... and update source so it is in sync with UI changes.
            sourceIndexPath = indexPath;
        }
        break;
    }

那你去吧!

您需要做的是立即阻止表视图更新,并使用beginUpdate和endUpdates同时执行所有更改。这样你就不会得到这样的错误。您还需要以相同的移动交换行,而不仅仅是移动它们。

我知道Ray Wenderlich的例子中他只提到移动的行,所以你走了!

http://www.raywenderlich.com/63089/cookbook-moving-table-view-cells-with-a-long-press-gesture