2011-03-01 55 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];