UITableView行高不变

时间:2017-12-22 04:42:50

标签: ios objective-c uitableview nslayoutconstraint

我创建了一个自定义Cell。我有一系列字典。对于字典值,我需要创建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];

为什么细胞高度没有变化?请帮我。 感谢

5 个答案:

答案 0 :(得分:0)

设置tableview的委托和数据源后,试试这个

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableViewAutomaticDimension //return height size whichever you want
    }

func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
    return 200 //return height size whichever you want
}

答案 1 :(得分:0)

请按照以下代码:

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
     return [self getMessageSize:[[_arrCaseActivityList objectAtIndex:indexPath.row] objectForKey:@"message"] Width:275];
}

-(float )getMessageSize :(NSString *)text Width :(float)textWidth
{
    NSAttributedString *attributedText =
[[NSAttributedString alloc]
 initWithString:text
 attributes:@
 {
 NSFontAttributeName: [UIFont fontWithName:@"Rubik-Regular" size:13.0]
 }];

    CGRect rect = [attributedText boundingRectWithSize:(CGSize){textWidth, CGFLOAT_MAX}
                                           options:NSStringDrawingUsesLineFragmentOrigin
                                           context:nil];
    CGSize finalSize = rect.size;
    return finalSize.height+75;

}

答案 2 :(得分:0)

单击+拖动您的tableview并将viewcontroller视图设置为width width and equal height

#pragma mark tableview delegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [DATA count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    PartyListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    if (cell == nil) {
        // Load the top-level objects from the custom cell XIB.
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"PartyListCell" owner:self options:nil];
        cell = [topLevelObjects objectAtIndex:0];
    }

  cell.textLabel.text = [DATA objectAtIndex:indexPath.row];

    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 38.0;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
   }

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *tableviewHeader = [[[NSBundle mainBundle] loadNibNamed:@"PartyListHeaderView" owner:self options:nil] objectAtIndex:0];
    return tableviewHeader;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 38.0;
}

点击按钮加载你的nib文件

 if (popUpView== nil) {
        popUpView = [[[NSBundle mainBundle] loadNibNamed:@"PopupView" owner:self options:nil] objectAtIndex:0];
        [self.view addSubview:popUpView];
        [popUpView setDelegate:self];
        [popUpView setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    }

答案 3 :(得分:0)

在Interface Builder中提供适当的约束,例如。 leading, trailing等,并将UILabel的no of lines设置为 0 。在viewDidLoad方法中添加以下代码。

self.tableView.rowHeight = UITableViewAutomaticDimension
self.tableView.estimatedRowHeight = 300 // estimated row height for performance improvement

答案 4 :(得分:0)

就我而言,我使用的是功能:

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

代替

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

确保使用正确的功能。