UITableView 行高不变 [英] UITableView row height not changing

查看:31
本文介绍了UITableView 行高不变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个自定义单元格.我有一系列字典.对于字典值,我需要创建 UILables.每个单元格可能包含不同数量的 UILabel.所以在我的自定义 UITableViewCell 类中,我是这样做的.

I have created a custom Cell. I have an array of dictionaries. For the dictionary values I need to create UILables. Each cell may contained different number of UILabels. So in my custom UITableViewCell class I have done like this.

- (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 方法中,我已经做到了.

And in my UIViewController viewDidLoad method I have done this.

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];

为什么单元格高度没有变化?请帮我.谢谢

Why the cell height is not changing? Please help me. Thanks

推荐答案

就我而言,我使用的是函数 :

In my case, I was using the function :

func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
        return 100
    }

代替

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 100
    }

确保您使用的是正确的函数.

Make sure you are using the correct function.

这篇关于UITableView 行高不变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆