2011-03-01 51 views
0

我有下面的代码创建我的UITableViewCell ...旋转与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

回答

1

尝试更换:

buttonRight.autoresizingMask = UIViewAutoresizingFlexibleRightMargin; 

buttonRight.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin; 

从纵向旋转为横向时应该有所帮助。但是,硬编码的220会导致单元在横向模式下初始化时出现问题。

有四种情况需要考虑:

  1. 在纵向模式下
  2. 在纵向模式初始化初始化,然后旋转为横向。
  3. 在风景模式下初始化
  4. 在风景模式下初始化,然后旋转为纵向。

您还需要使用框架宽度并从右边减去按钮宽度以处理所有四种情况。

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