我正在尝试以编程方式创建一个自定义UITableViewCell
(没有XIB),该自定义包含一个UILabel
使用“自动布局”来指定单元格高度的单个。我已将此标签赋予单元格的前导,尾随,顶部和底部约束,contentView
但是这些约束不会影响表格视图单元的高度。标签全部堆叠在一起而没有填充,并且表格视图分隔线也不与标签对齐。这里有什么问题?
自定义单元:
@interface TransactionTableViewCell ()
@property (nonatomic, assign) BOOL didUpdateConstraints;
@end
@implementation TransactionTableViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
self.translatesAutoresizingMaskIntoConstraints = NO;
self.transactionPriceLabel = [[UILabel alloc] init];
self.transactionPriceLabel.backgroundColor = [UIColor yellowColor];
self.transactionPriceLabel.translatesAutoresizingMaskIntoConstraints = NO;
[self.contentView addSubview:self.transactionPriceLabel];
}
[self setNeedsUpdateConstraints]; //had to add this otherwise updateConstraints isn't called for some reason
return self;
}
- (void)updateConstraints {
if (!self.didUpdateConstraints) {
self.didUpdateConstraints = YES;
[self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.transactionPriceLabel
attribute:NSLayoutAttributeTrailing
relatedBy:NSLayoutRelationEqual
toItem:self.contentView
attribute:NSLayoutAttributeTrailing
multiplier:1
constant:-15]];
[self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.transactionPriceLabel
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:self.contentView
attribute:NSLayoutAttributeTop
multiplier:1
constant:15]];
[self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.transactionPriceLabel
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:self.contentView
attribute:NSLayoutAttributeBottom
multiplier:1
constant:15]];
[self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.transactionPriceLabel
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:self.contentView
attribute:NSLayoutAttributeLeading
multiplier:1
constant:15]];
}
[super updateConstraints];
}
查看控制器:
- (void)viewDidLoad {
UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
tableView.dataSource = self;
tableView.delegate = self;
tableView.rowHeight = UITableViewAutomaticDimension;
tableView.estimatedRowHeight = 44.0;
[self.view addSubview:tableView];
tableView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:tableView
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeLeading
multiplier:1
constant:0]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:tableView
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeTop
multiplier:1
constant:0]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:tableView
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeBottom
multiplier:1
constant:0]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:tableView
attribute:NSLayoutAttributeTrailingMargin
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeTrailingMargin
multiplier:1
constant:0]];
[tableView registerClass:[TransactionTableViewCell class] forCellReuseIdentifier:@"cell"];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
TransactionTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
cell.transactionPriceLabel.text = @"label text";
cell.transactionPriceLabel.textColor = [UIColor redColor];
return cell;
}
标签底部到contentView约束底部的常数应为-15,而不是15。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句