UITableView重新排序按钮覆盖整个单元格

时间:2013-04-21 17:45:16

标签: ios objective-c uitableview cgaffinetransform

我一直在尝试使用this tutorial使重新排序按钮覆盖整个单元格。它很有效,直到单元格从视图中消失。一旦你回到单元格,重新排序按钮已经转移了很多。

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

这是本教程中使用的代码。

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

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

1 个答案:

答案 0 :(得分: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];