簡體   English   中英

使用UITableViewCell旋轉

[英]Rotation with 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中創建整個組件,這是否可能?

在此處輸入圖片說明

您應該能夠通過計算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];

嘗試更換:

buttonRight.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;

buttonRight.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;

從縱向旋轉到橫向旋轉時,這應該會有所幫助。 然而,當以橫向模式初始化單元時,硬編碼的220將引起問題。

有四種情況需要考慮:

  1. 以人像模式初始化
  2. 以人像模式初始化,然后旋轉為橫向。
  3. 在橫向模式下初始化
  4. 在橫向模式下初始化,然后旋轉為縱向。

您還需要使用框架寬度並從右邊緣減去按鈕寬度來處理所有四種情況。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM