重新排序后,UITableViewCells在顶部滚动

时间:2013-04-27 03:17:55

标签: ios objective-c uitableview

在重新排序UITableViewCell并滚动tableView之后,单元格会在标题的顶部而不是在其下方。在我的tableView中,其编辑属性始终设置为YES,并且重新排序控件被拉伸以覆盖整个单元格。我已经尝试sendSubviewToBack:将单元格发送到后面,bringSubviewToFront:将标题放到前面,但它不起作用。

以下是调整重新排序控件的代码:

- (void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    JRBTask *task = [[[JRBTaskStore sharedStore] taskArrayWithIndex: indexPath] objectAtIndex: [indexPath row]];

    if (![task reorderControlIsResized]) {
        //  Grip customization code goes in here...
        for(UIView* view in cell.subviews)
        {
            if ([[[view class] description] isEqualToString:@"UITableViewCellEditControl"])
                [view removeFromSuperview];
            if([[[view class] description] isEqualToString:@"UITableViewCellReorderControl"])
            {
                UIView* resizedGripView = [[UIView alloc] init];

                // Use initial frame so it's resizing from its initial
                // position each time, not from its current position
                if (!initialFrame.size.height)
                    [resizedGripView setFrame: CGRectMake(0, 0, CGRectGetMaxX(view.frame), CGRectGetMaxY(view.frame))];
                else
                    [resizedGripView setFrame: initialFrame];

                if (!initialFrame.size.height)
                    [self setInitialFrame: resizedGripView.frame];

                [resizedGripView addSubview:view];
                [cell addSubview:resizedGripView];

                CGSize sizeDifference = CGSizeMake(initialFrame.size.width - view.frame.size.width, initialFrame.size.height - view.frame.size.height);
                CGSize transformRatio = CGSizeMake(initialFrame.size.width / view.frame.size.width, initialFrame.size.height / view.frame.size.height);

                //  Original transform
                CGAffineTransform transform = CGAffineTransformIdentity;

                //  Scale custom view so grip will fill entire cell
                transform = CGAffineTransformScale(transform, transformRatio.width, transformRatio.height);

                transform = CGAffineTransformTranslate(transform, -139, -sizeDifference.height / 2.0);

                [resizedGripView setTransform: transform];

                for(UIImageView* cellGrip in view.subviews)
                {
                    if([cellGrip isKindOfClass:[UIImageView class]])
                         [cellGrip setImage:nil];
                }
                reorderNeedsAdjusted = NO;

            }
        }
    }
}

在单元格移动时使用tableView:targetIndexPathForMoveFromRowAtIndexPath:toProposedIndexPath:管理更新,并在移动单元格后使用tableView:moveRowAtIndexPath:toIndexPath:管理更新。

-(NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{    
    return proposedDestinationIndexPath;
}

-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
    [[JRBTaskStore sharedStore] moveTaskFromIndex: sourceIndexPath toIndex: destinationIndexPath];

    // Update number of rows in section if the section is different
    if ([sourceIndexPath section] != [destinationIndexPath section]) {
        // Subtract from source section
        if ([sourceIndexPath section] == 0)
            numberOfRowsInToday--;
        else if ([sourceIndexPath section] == 1)
            numberOfRowsInTomorrow--;
        else if ([sourceIndexPath section] == 2)
            numberOfRowsInUpcoming--;
        else
            numberOfRowsInSomeday--;

        // Add to destination section
        if ([destinationIndexPath section] == 0)
            numberOfRowsInToday++;
        else if ([destinationIndexPath section] == 1)
            numberOfRowsInTomorrow++;
        else if ([destinationIndexPath section] == 2)
            numberOfRowsInUpcoming++;
        else
            numberOfRowsInSomeday++;
    }
}

Issue

1 个答案:

答案 0 :(得分:0)

我知道这不是最好的答案。但是,它对我有用。 这个问题在IOS7之后解决了。

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
    UITableViewCell * movedCell = [tableView cellForRowAtIndexPath:sourceIndexPath];
    double delayInSeconds = 0.3;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        [tableView sendSubviewToBack:movedCell];
    });
}

我希望这会对某人有所帮助:)。