Search code examples
iosobjective-cuitableviewcgaffinetransform

UITableView reorder button covering the entire cell


I've been trying to use this tutorial to make the reorder button cover the entire cell. It works great until the cell disappears from the view. Once you go back to the cell, the reorder button has shifted over quite a bit.

In this picture, the red represents the reorder button. pic

Here's the code used in the tutorial.

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

How do I keep the reorder control from moving to the left? I've tried to translate the transform again, but that just makes it so the reorder control is completely off the screen. What's wrong with the code that makes it move to the left and how do I fix it?


Solution

  • I figured out how to do it! I had to add a property to the viewController that stored the initial frame of the resizedGripView. It turns out that every time the method was being called (every time the cell appeared again), the reorder button was being moved from it's current position, so I had to store it's initial position.

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