使用UITableViewCell旋转

时间:2011-03-01 21:53:33

标签: iphone uitableview

我有以下代码创建我的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;
}

这在纵向上工作正常但在横向旋转时,UIButton“buttonRight”不会停留在附件视图旁边。如果没有在IB中创造整个事物,这可能吗?

enter image description here

2 个答案:

答案 0 :(得分:2)

您应该能够通过计算x位置来实现这一目标,而不是将其硬编码为220.0。定义图像宽度和高度的一些常量和缓冲区。

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

答案 1 :(得分:1)

尝试更换:

buttonRight.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;

buttonRight.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;

从纵向旋转到横向时,这应该会有所帮助。但是,当以横向模式初始化单元时,硬编码220将导致问题。

有四种情况需要考虑:

  1. 以纵向模式初始化
  2. 以纵向模式初始化,然后旋转至横向。
  3. 以横向模式初始化
  4. 以横向模式初始化,然后旋转为纵向。
  5. 您还需要使用框架宽度并从右边缘减去按钮宽度来处理所有四种情况。