【问题标题】:how to fix the height of a tableview cell如何修复表格视图单元格的高度
【发布时间】:2015-05-22 08:48:33
【问题描述】:

最近我写了一个自定义的UITableViewCell,高度为60.0f。当它在不同的设备上运行时,例如iPhone5iPhone6iPhone6 +,高度和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;
}

【问题讨论】:

  • 请显示tableview创建代码,可能你需要将UITableView的rowHeight改为60.0f而不是UITableViewAutomaticDimension
  • 对不起,这里是创作:
  • - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 60.0; }

标签: ios uitableview autolayout font-size fixed


【解决方案1】:

self.detailTextLabel.numberOfLines 设置为精确值,而不是 0。这将关闭标签的自动调整大小。

【讨论】:

  • 然后尽量不要使用 UITableViewCell 中的 textLabel 和 detailTextLabel。在您自己的属性中设置您自己的标签。
猜你喜欢
  • 1970-01-01
  • 2021-01-21
  • 1970-01-01
  • 2018-06-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-06
相关资源
最近更新 更多