Lee Armstrong
Lee Armstrong

Reputation: 11452

Rotation with UITableViewCell

I have the following code that creates my UITableViewCell...

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSNumberFormatter *numberFormatter = nil;
    if (numberFormatter == nil) {
        numberFormatter = [[NSNumberFormatter alloc] init];
        [numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
        [numberFormatter setMaximumFractionDigits:3];
    }

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];

        UIImage *image = [UIImage imageNamed:@"home1"];
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        CGRect frame = CGRectMake(0.0, 0.0, image.size.width, image.size.height);
        button.frame = frame;
        button.tag = indexPath.row;
        [button setBackgroundImage:image forState:UIControlStateNormal];
        [button addTarget:self action:@selector(setHomeButtonTapped:)  forControlEvents:UIControlEventTouchUpInside];
        button.backgroundColor = [UIColor clearColor];
        cell.accessoryView = button;

        UIImage *imageShare = [UIImage imageNamed:@"arrows-share"];
        UIButton *buttonRight = [UIButton buttonWithType:UIButtonTypeCustom];
        buttonRight.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;
        [buttonRight setBackgroundImage:imageShare forState:UIControlStateNormal];
        [buttonRight setFrame: CGRectMake( 220.0f, 0.0f, imageShare.size.width, imageShare.size.height)];
        [buttonRight addTarget:self action:@selector(shareLink:) forControlEvents:UIControlEventTouchUpInside];

        [cell addSubview:buttonRight];
    }

    Event *event = (Event *)[eventsArray objectAtIndex:indexPath.row];

    cell.textLabel.text = [event name];

    NSString *string = [NSString stringWithFormat:@"%@, %@",
                        [numberFormatter stringFromNumber:[event lat]],
                        [numberFormatter stringFromNumber:[event lon]]];
    cell.detailTextLabel.text = string;

    return cell;
}

This works fine in portrait but in Landscape when rotated the UIButton "buttonRight" doesn't stay next to the accessory View. Is this possible without creating the whole thing in IB?

enter image description here

Upvotes: 0

Views: 967

Answers (2)

John Lemberger
John Lemberger

Reputation: 2699

Try replacing:

buttonRight.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;

with

buttonRight.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;

That should help when rotating from portrait to landscape. However, the hard-coded 220 will cause problems when the cells are initialized in landscape mode.

There are four cases to consider:

  1. Initialized in portrait mode
  2. Initialized in portrait mode then rotated to landscape.
  3. Initialized in landscape mode
  4. Initialized in landscape mode then rotated to portrait.

You'll also need to use the frame width and subtract the button width from the right edge to handle all four cases.

Upvotes: 1

Mark Adams
Mark Adams

Reputation: 30846

You should be able to achieve this by calculating your x position, instead of hardcoding it at 220.0. Define some constants for the width and height of your images and a buffer.

CGPointMake buttonOrigin = CGPointMake(cell.frame.size.width - kHomeButtonWidth - kShareButtonWidth - (kBufferWidth * 2), (cell.frame.size.height - kShareButtonHeight) / 2);
CGRect buttonFrame = CGRectMake(buttonOrigin.x, buttonOrigin.y, kShareButtonWidth, kShareButtonHeight);
[buttonRight setFrame:buttonFrame];

Upvotes: 2

Related Questions