ios - Objective-C:以编程方式创建的 UITableViewCell 高度问题?

标签 ios objective-c uitableview constraint-programming

如何在运行时根据来自响应的内容调整 UITableViewCell 的高度

这是我的代码:

.h

@interface ViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>    
@end

.m

import "ViewController.h"
import "CustomCellTableViewCell.h"

static NSString *cellIdentifier = @"cellIdentifier";


@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self cofigureTableview];
}

-(void)cofigureTableview
{
    self.table = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.table.delegate = self;
    self.table.dataSource = self;
    [self.view addSubview:self.table];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
      return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
      return 2;
}

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

    CustomCellTableViewCell *cell = [self.table dequeueReusableCellWithIdentifier:cellIdentifier];
    cell = [[CustomCellTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    [self setUpCell:cell atIndexPath:indexPath];

    return cell;
}

- (void)setUpCell:(CustomCellTableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
     if (cell != nil)
     {
        UILabel *averageRatingLabel = [[UILabel alloc]init];
        averageRatingLabel.text = @"Average Rate";
        averageRatingLabel.textAlignment = NSTextAlignmentLeft;
        averageRatingLabel.numberOfLines = 0;
        averageRatingLabel.lineBreakMode = NSLineBreakByWordWrapping;
        averageRatingLabel.textColor = [UIColor blackColor];
        averageRatingLabel.translatesAutoresizingMaskIntoConstraints = NO;
        averageRatingLabel.font = [UIFont systemFontOfSize:12];
        [cell.contentView addSubview:averageRatingLabel];

        /////
        UILabel *sortByLabel = [[UILabel alloc]init];
        sortByLabel.text = @"SORT BY Value:";
        sortByLabel.textAlignment = NSTextAlignmentLeft;
        sortByLabel.numberOfLines = 0;
        sortByLabel.lineBreakMode = NSLineBreakByWordWrapping;
        sortByLabel.textColor = [UIColor grayColor];
        sortByLabel.translatesAutoresizingMaskIntoConstraints = NO;
        sortByLabel.font = [UIFont systemFontOfSize:10];
        [cell.contentView addSubview:sortByLabel];


        UILabel *sortByLabel1 = [[UILabel alloc]init];
        sortByLabel1.text = @"I want to adjust the row height of a UITableView according to the cell in that row. I want to adjust the row height of a UITableView according to the cell in that row.I want to adjust the row height of a UITableView according to the cell in that row";

        sortByLabel1.textAlignment = NSTextAlignmentLeft;
        sortByLabel1.numberOfLines = 0;
        sortByLabel1.lineBreakMode = NSLineBreakByWordWrapping;
        sortByLabel1.textColor = [UIColor grayColor];
        sortByLabel1.translatesAutoresizingMaskIntoConstraints = NO;
        sortByLabel1.font = [UIFont systemFontOfSize:10];
        [cell.contentView addSubview:sortByLabel1];

        UILabel *sortByLabel2 = [[UILabel alloc]init];
        sortByLabel2.text = @"Is there any method to achieve this:";
        sortByLabel2.textAlignment = NSTextAlignmentLeft;
        sortByLabel2.numberOfLines = 0;
        sortByLabel2.lineBreakMode = NSLineBreakByWordWrapping;
        sortByLabel2.textColor = [UIColor grayColor];
        sortByLabel2.translatesAutoresizingMaskIntoConstraints = NO;
        sortByLabel2.font = [UIFont systemFontOfSize:10];
        [cell.contentView addSubview:sortByLabel2];

        UILabel *sortByLabel3 = [[UILabel alloc]init];
        sortByLabel3.text = @"This should work because tableView: cellForRowAtIndexPath:";
        sortByLabel3.textAlignment = NSTextAlignmentLeft;
        sortByLabel3.numberOfLines = 0;
        sortByLabel3.lineBreakMode = NSLineBreakByWordWrapping;
        sortByLabel3.textColor = [UIColor grayColor];
        sortByLabel3.translatesAutoresizingMaskIntoConstraints = NO;
        sortByLabel3.font = [UIFont systemFontOfSize:10];
        [cell.contentView addSubview:sortByLabel3];

        UILabel *sortByLabel4 = [[UILabel alloc]init];
        sortByLabel4.text = @"then in your tableView:  in your viewDidAppearthen in your tableView: cellForRowAtIndexPath: update this variable like your new height and then reload the [self.yourTable reloadData] in your viewDidAppearthen in your tableView: cellForRowAtIndexPath: update this variable like your new height and then reload the [self.yourTable reloadData] in your viewDidAppear:";
        sortByLabel4.textAlignment = NSTextAlignmentLeft;
        sortByLabel4.numberOfLines = 0;
        sortByLabel4.lineBreakMode = NSLineBreakByWordWrapping;
        sortByLabel4.textColor = [UIColor grayColor];
        sortByLabel4.translatesAutoresizingMaskIntoConstraints = NO;
        sortByLabel4.font = [UIFont systemFontOfSize:10];
        [cell.contentView addSubview:sortByLabel4];


        NSDictionary *viewDict1 = NSDictionaryOfVariableBindings(averageRatingLabel);
        [cell.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-5-[averageRatingLabel]|" options:0 metrics:0 views:viewDict1]];
       [cell.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-5-[averageRatingLabel]" options:0 metrics:0 views:viewDict1]];

        NSDictionary *viewDict2 = NSDictionaryOfVariableBindings(sortByLabel,averageRatingLabel);
        [cell.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-5-[sortByLabel]-5-|" options:0 metrics:0 views:viewDict2]];
        [cell.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[averageRatingLabel]-5-[sortByLabel]" options:0 metrics:0 views:viewDict2]];

        NSDictionary *viewDict3 = NSDictionaryOfVariableBindings(sortByLabel,sortByLabel1);
       [cell.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-5-[sortByLabel1]-5-|" options:0 metrics:0 views:viewDict3]];
       [cell.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[sortByLabel]-5-[sortByLabel1]" options:0 metrics:0 views:viewDict3]];

        //
        NSDictionary *viewDict4 = NSDictionaryOfVariableBindings(sortByLabel1,sortByLabel2);
       [cell.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-5-[sortByLabel2]-5-|" options:0 metrics:0 views:viewDict4]];
       [cell.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[sortByLabel1]-5-[sortByLabel2]" options:0 metrics:0 views:viewDict4]];

        //
        NSDictionary *viewDict5 = NSDictionaryOfVariableBindings(sortByLabel2,sortByLabel3);


        [cell.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-5-[sortByLabel3]-5-|" options:0 metrics:0 views:viewDict5]];
        [cell.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[sortByLabel2]-5-[sortByLabel3]" options:0 metrics:0 views:viewDict5]];

        //
        NSDictionary *viewDict6 = NSDictionaryOfVariableBindings(sortByLabel3,sortByLabel4);
        [cell.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-5-[sortByLabel4]-5-|" options:0 metrics:0 views:viewDict6]];
        [cell.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[sortByLabel3]-5-[sortByLabel4]" options:0 metrics:0 views:viewDict6]];

     }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static CustomCellTableViewCell *cell = nil;
    static dispatch_once_t onceToken;

    dispatch_once(&onceToken, ^{

        cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    });
    [self setUpCell:cell atIndexPath:indexPath];

    return [self calculateHeightForConfiguredSizingCell:cell];

}

- (CGFloat)calculateHeightForConfiguredSizingCell:(UITableViewCell *)sizingCell
{

    [sizingCell layoutIfNeeded];
    CGSize size = [sizingCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
    return size.height;

}


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

}


- (void)didReceiveMemoryWarning
{

    [super didReceiveMemoryWarning];

}

@end

最佳答案

使用此代码根据内容大小自动调整单元格高度:

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

- (CGFloat) tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 300.0f;
}

你也可以这样使用它:

self.tableView.estimatedRowHeight = 300.0;
self.tableView.rowHeight = UITableViewAutomaticDimension;

关于ios - Objective-C:以编程方式创建的 UITableViewCell 高度问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40927932/

相关文章:

ios - 滚动时表格 View 数据重复

ios - Objective-c, Action 完成后未删除SpriteKit粒子

ios - 如何制作自定义高度的进度条?

ios - MapKit Xcode 编程

ios - 数据服务错误未解决

ios - 滚动时 UITableView 不刷新

iphone - 调用另一个 UIViewController

ios - 如何使用 UIButton 更改容器的内容?

ios - UITableView 多个单元格

ios - 无法将类型 'SKTexture' 的值转换为预期的参数类型 'Int' ;使用textureNamed时出错 ("...")