簡體   English   中英

如何確定表格單元格的高度

[英]how to fix the height of a tableview cell

最近,我編寫了一個高度為60.0f的自定義UITableViewCell 當它在其他設備(例如iPhone5 iPhone6 iPhone6 + ,高度和fontSize會自動更改。 如何確定高度?

  - (id)initWithStyle:(UITableViewCellStyle)style
        reuseIdentifier:(NSString *)reuseIdentifier
    {
        self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
        if (!self) {
            return nil;
        }       
        self.textLabel.adjustsFontSizeToFitWidth = NO;
        self.textLabel.font = [UIFont systemFontOfSize:15.0f];
        self.detailTextLabel.adjustsFontSizeToFitWidth = NO;
        self.detailTextLabel.font = [UIFont systemFontOfSize:12.0f];
        self.detailTextLabel.numberOfLines = 0;
        _switchButton = [[UISwitch alloc] initWithFrame:CGRectMake(250, 9, 30, 20)];
        _switchButton.tag = 400;
        [_switchButton addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_switchButton];

        _button = [UIButton buttonWithType:UIButtonTypeCustom];
        _button.frame = CGRectMake(250, 9, 60, 30);
        [_button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_button];
        _button.backgroundColor = [UIColor clearColor];

        return self;
    }

在控制器中:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60.0;
}

self.detailTextLabel.numberOfLines設置為精確值,而不是0。這將關閉標簽的自動大小調整。

暫無
暫無

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

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