1
我試圖以編程方式創建自定義UITableViewCell
,不包含XIB,它由使用自動佈局指定單元格高度的單個UILabel
組成。我已經將該標籤的前導,尾部,頂部和底部約束條件賦予單元格的contentView
,但這些約束條件不影響表格視圖單元格的高度。標籤都堆疊在一起,沒有填充物,表格視圖分隔線也不與標籤對齊。這裏有什麼問題?具有自動佈局約束的UITableViewCell子類尺寸不正確
自定義單元格:
@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;
}
好眼!哇,這很簡單。 – Joey 2015-02-07 23:54:00