当用户的手指离开屏幕时,UITableView返回错误的indexPath

时间:2015-08-26 13:47:20

标签: ios objective-c iphone uitableview reorderlist

我正在开发一个重新排序的单元格类,以允许我的用户重新排序表格中的单元格。

这个过程非常简单,我每次操作其中一个时都会切换单元格的位置。

这个过程非常顺利,直到我在iPhone屏幕外面伸出手指。在这种情况下,UIGestureRecognizer或UITableView给我一个错误的indexPath。它指的是我的tableView的第一个位置。

这是我的代码:

初始化手势识别

...
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]
                                           initWithTarget:self action:@selector(longPressGestureRecognized:)];

[_tableView addGestureRecognizer:longPress];

...

控制手势状态的方法

- (IBAction)longPressGestureRecognized:(id)sender {

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

    CGPoint location = [longPress locationInView:_tableView];
    NSIndexPath *indexPath = [_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.

    NSLog(@"State: %ld", (long)state);

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

                Task *sourceTask = [_elements objectAtIndex:sourceIndexPath.row];
                NSLog(@"superTask: state[%ld], %@, indexpath.row: %@, actualPosition: %@", (long)state, sourceTask.subtask, @(indexPath.row), sourceTask.position);

                if (_collapseSubElements){
                    if ([sourceTask.subtask isEqualToString:@"0"]){
                        [self collapseAllTaskWithSubtasks];
                        sourceIndexPath = indexPath = [NSIndexPath indexPathForRow:[_elements indexOfObjectIdenticalTo:sourceTask] inSection:0];
                    }

                    UITableViewCell *cell = [_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;
                    [_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;

            Task *sourceTask = [_elements objectAtIndex:sourceIndexPath.row];
            Task *targetTask = [_elements objectAtIndex:indexPath.row];

            NSLog(@"UIGestureRecognizerStateChanged: %ld - %ld -  %@", (long)indexPath.row, (long)sourceIndexPath.row, sourceTask.position);

            // Is destination valid and is it different from source?
            if (indexPath && ![indexPath isEqual:sourceIndexPath]) {
                if (indexPath.row - sourceIndexPath.row <= 1){

                    UITableViewCell *cell = [_tableView cellForRowAtIndexPath:indexPath];

                    UIButton *subtasksButton = (UIButton *)[cell viewWithTag:108];

                    // Solamente se pueden mover tareas dentro del mismo nivel
                    if ([sourceTask isBrotherOfTask:targetTask]){
                        if (_collapseSubElements){
                            if (subtasksButton.selected){
                                //[self expandCollapseSubtasks:subtasksButton];
                                _collapseCellsBlock(subtasksButton);
                            }

                            // ... update element position
                            NSNumber *sourceTaskPosition = sourceTask.position;
                            NSNumber *targetTaskPosition = targetTask.position;

                            _temporalValues = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                               sourceTaskPosition, @"position", nil];
                            [targetTask updateWithValues: _temporalValues];

                            _temporalValues = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                               targetTaskPosition, @"position", nil];
                            [sourceTask updateWithValues: _temporalValues];

                            // ... update data source.
                            [_elements exchangeObjectAtIndex:indexPath.row withObjectAtIndex:sourceIndexPath.row];

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

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

                        }
                    }
                }

            }
            break;
        }

        case UIGestureRecognizerStateEnded:
        {
            NSLog(@"UIGestureRecognizerStateEnded");

            Task *task = [_elements objectAtIndex:indexPath.row];
            //if (indexPath && ![indexPath isEqual:sourceIndexPath]) {
            NSLog(@"httpClient indexpath.row: %@, editedposition: %@", @(indexPath.row), task.position);
            /*[_httpClient createUpdateRequestForObject:task withPath:@"task/" withRegeneration:NO];
            [_httpClient update:nil];*/
            _completionBlock(task);

        }

        default: {
            // Clean up.
            UITableViewCell *cell = [_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;
        }
    }
}

从所选单元格创建快照的方法

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

细胞运动 当我移动单元格8时一切正常,但当我将单元格6移动到底部时,界面将单元格保持在正确位置但返回的indexPath为0

enter image description here

1 个答案:

答案 0 :(得分:2)

我认为原因是来自_tableView的contentize小于你指向的位置。

您可以查看:

     if (_tableView.contentSize.height<location.y) indexPath = [NSIndexPath indexPathForRow:[_tableView numberOfRowsInSection:0]-1 inSection:0];