在节之间移动uitableview单元格时添加长按手势



以下是我前面的问题moveRowAtIndexPath-在区段之间移动单元格

我已经能够做一个传统的moveRowAtIndexPath来在部分之间移动单元格,即:将标记从Sales移动到Marketing。

然而,现在我想更进一步,在表视图上添加长按手势,并允许我从简单地在单元格上使用长按移动单元格。

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

我的排是这样的;

-(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];
    }
}

因此,现在我在查看DidLoad时将长按添加到表中,ala;

UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGestureRecognized:)];
[self.tableView addGestureRecognizer: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中崩溃了,它试图移动细胞;这与移动后/移动前数组不一致有关。

此外,我不确定我做得是否正确;这种模式能在基于部门和员工的列表中工作吗?

无论如何,我在这里的问题是:如何将长按手势添加到由部分组成的表视图单元格中,或者。。我想用长按把汤姆从销售部门调到市场部。

给定上面的代码,这可能吗?

编辑;

我得到的崩溃是:

"NSInternalConferenceException",原因:"无效更新:无效节0中的行数。包含在更新后的现有节(3)必须等于更新(3)之前该节中包含的行,加或减从该部分插入或删除的行数(插入0,0已删除),加上或减去移入或移出的行数该节(0已移入,1已移出)。'***第一次投掷呼叫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和此代码都在重复相同的代码。

但我可以稍后清理。

感谢

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

最新更新