0

我一直在尝试使用本教程使重新排序按钮覆盖整个单元格。它工作得很好,直到单元格从视图中消失。一旦你回到单元格,重新排序按钮已经移动了很多。

在这张图片中,红色代表重新排序按钮。 图片

这是教程中使用的代码。

- (void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    //  Grip customization code goes in here...
    for(UIView* view in cell.subviews)
    {
         if([[[view class] description] isEqualToString:@"UITableViewCellReorderControl"])
        {
                UIView* resizedGripView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetMaxX(view.frame), CGRectGetMaxY(view.frame))];
            [resizedGripView addSubview:view];
            [cell addSubview:resizedGripView];
            [resizedGripView release];

            CGSize sizeDifference = CGSizeMake(resizedGripView.frame.size.width - view.frame.size.width, resizedGripView.frame.size.height - view.frame.size.height);
            CGSize transformRatio = CGSizeMake(resizedGripView.frame.size.width / view.frame.size.width, resizedGripView.frame.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);

            //  Move custom view so the grip's top left aligns with the cell's top left 
            transform = CGAffineTransformTranslate(transform, -sizeDifference.width / 2.0, -sizeDifference.height / 2.0);

            [resizedGripView setTransform:transform];

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

如何防止重新排序控件向左移动?我试图再次翻译转换,但这只是让重新排序控制完全脱离屏幕。使它向左移动的代码有什么问题,我该如何解决?

4

1 回答 1

0

我想出了怎么做!我必须向 viewController 添加一个属性,该属性存储了 resizedGripView 的初始帧。事实证明,每次调用该方法时(每次单元格再次出现),重新排序按钮都会从其当前位置移动,因此我必须存储它的初始位置。

UIView* resizedGripView = [[UIView alloc] init];
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];
于 2013-04-25T12:13:52.033 回答