我创建了一个自定义单元格。我有一系列字典。对于我需要创建的字典值UILable
s。每个单元可能包含不同数量的UILabel
s。所以按照我的习惯UITableViewCell
类我就是这样做的。
- (void)generateCell {
BOOL isLandscape = UIInterfaceOrientationIsLandscape(vc.interfaceOrientation);
for (int i = 0; i < [orderMap count]; i++) {
UILabel *lbl = [[UILabel alloc] init];
[lbl setTextColor:[UIColor blueColor]];
[lbl setBackgroundColor:[UIColor yellowColor]];
lbl.tag = [[orderMap objectAtIndex:i] intValue];
[lbl setNumberOfLines:0];
[self.contentView addSubview:lbl];
[lbl setTranslatesAutoresizingMaskIntoConstraints:false];
}
if (isLandscape) {
}
else {
[self setConstraintsForPortrait];
}
}
- (void)setConstraintsForPortrait {
double currentUsedWidth = 0;
double relativeWidthLimit = 1;
int reservedPaddingSpace = 20;
int usableScreenWidthPts = 0;
int xPositionStartingPoint = 15;
int xPosition = xPositionStartingPoint;
int yPosition = INDIVIDUAL_PADDING_PTS;
int fieldCounter = 0;
UIView *previousView;
for (UIView *vw in [self.contentView subviews]) {
NSLayoutConstraint *leading = [NSLayoutConstraint constraintWithItem:vw attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeLeading multiplier:1.0 constant:(xPosition + INDIVIDUAL_PADDING_PTS)];
NSLayoutConstraint *top;
if (previousView == nil) {
top = [NSLayoutConstraint constraintWithItem:vw attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeTop multiplier:1.0 constant:INDIVIDUAL_PADDING_PTS];
}
else {
top = [NSLayoutConstraint constraintWithItem:vw attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:previousView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:INDIVIDUAL_PADDING_PTS];
}
NSLayoutConstraint *trailing = [NSLayoutConstraint constraintWithItem:vw attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:INDIVIDUAL_PADDING_PTS];
if (fieldCounter >= [orderMap count]) {
NSLayoutConstraint *bottom = [NSLayoutConstraint constraintWithItem:vw attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:INDIVIDUAL_PADDING_PTS];
[self.contentView addConstraint:bottom];
}
[self.contentView addConstraint:leading];
[self.contentView addConstraint:top];
[self.contentView addConstraint:trailing];
previousView = vw;
}
}
而在我的UIViewController
viewDidLoad
方法我已经这样做了。
UINib *nib = [UINib nibWithNibName:@"TableViewCell" bundle:[NSBundle mainBundle]];
[self.tbl registerNib:nib forCellReuseIdentifier:metadataListTableIdentifier];
self.tbl.estimatedRowHeight = 200;
self.tbl.rowHeight = UITableViewAutomaticDimension;
[self.tbl reloadData];
为什么像元高度没有改变?请帮我。
谢谢
设置tableview的委托和数据源后,试试这个
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension //return height size whichever you want
}
or
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return 200 //return height size whichever you want
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)