ios - 我们如何根据字段内容大小增加uitableviewcell的高度

标签 ios objective-c

嗨,我是iOS的新手,在我的项目中,我在UIButton上添加了两个UITextView和一个UITableViewCell,由于数据来自服务,因此UITextView基于内容大小而增长。

我的要求基于textSize-为此必须增加像元高度。我已经写了一些代码,但是没有用。我在这里做错了什么?请帮助我,我已经尝试了很长时间,但是我没有得到结果。

我的代码:

- (void)viewDidLoad {
    [super viewDidLoad];

    tableList = [[UITableView alloc]init];
    tableList.translatesAutoresizingMaskIntoConstraints = NO;
    tableList.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    tableList.dataSource=self;
    tableList.delegate=self;
    tableList.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    [tableList registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
    [self.view addSubview:tableList];

    NSDictionary * views = NSDictionaryOfVariableBindings(tableList);

    NSArray * horizentalConstraint = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[tableList]-0-|" options:0 metrics:nil views:views];

    NSArray * verticalConstraint = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[tableList]-0-|"options:0 metrics:nil views:views];

    [self.view addConstraints:horizentalConstraint];
    [self.view addConstraints:verticalConstraint];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return 10;
}

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

    return UITableViewAutomaticDimension
}

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

    static NSString *cellIdentifier = @"Cell";

    UITableViewCell *Cell =[tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    Cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];


UITextView * aboutText = [[UITextView alloc]init];
        aboutText.font = [UIFont fontWithName:@"Bitter-Regular" size:15.0f];
        aboutText.translatesAutoresizingMaskIntoConstraints = NO;
        aboutText.backgroundColor = [UIColor darkGrayColor];
        aboutText.scrollEnabled = NO;
        [Cell.contentView addSubview:aboutText];

        UIButton * button1 = [[UIButton alloc]init];
        button1.translatesAutoresizingMaskIntoConstraints = NO;
        button1.backgroundColor = [UIColor redColor];
        [Cell.contentView addSubview:button1];

        UIButton * button2 = [[UIButton alloc]init];
        button2.translatesAutoresizingMaskIntoConstraints = NO;
        button2.backgroundColor = [UIColor orangeColor];
        [Cell.contentView addSubview:button2];

        NSDictionary * views = NSDictionaryOfVariableBindings(aboutText,button1,button2);

        NSArray * formulaH = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10-[aboutText]-10-|"
                                                                     options:0
                                                                     metrics:nil
                                                                       views:views];


        NSArray * button1H = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10-[button1]-10-|"
                                                                     options:0
                                                                     metrics:nil
                                                                       views:views];

        NSArray * button2H = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10-[button2]-10-|"
                                                                     options:0
                                                                     metrics:nil
                                                                       views:views];

        NSArray * verticalspacing = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-5-[aboutText]-10-[button1(30)]-10-[button2(30)]"
                                                                            options:0
                                                                            metrics:nil
                                                                              views:views];

        [Cell.contentView addConstraints:formulaH];
        [Cell.contentView addConstraints:button1H];
        [Cell.contentView addConstraints:button2H];
        [Cell.contentView addConstraints:verticalspacing];

        aboutText.text = @"Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.";

return Cell;

}

最佳答案

tableview的这种行为称为tableviewcell的动态自定义大小。如图所示,设置按钮和textview的约束。禁用textview滚动。
viewDidLoad()中添加以下内容

tableView.estimatedRowHeight = 44.0;
tableView.rowHeight = UITableViewAutomaticDimension;

enter image description here
enter image description here

关于ios - 我们如何根据字段内容大小增加uitableviewcell的高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32713969/

相关文章:

ios - Swift 闭包作为字典中的值

ios - 代码在模拟器中运行,在设备上运行时抛出 dyld symbol not found 错误(Xcode 6 beta)

ios - Swift - 以编程方式实例化 View Controller 时缺少元素

iphone - 行数为 0 时 UITableView 中的 EXC_BAD_ACCESS 崩溃

objective-c - NSURLConnection 线程安全吗?

ios - 取消存档数据报告错误: Thread 1:EXC_BREAKPOINT(code=EXC_BREAKPOINT,子代码=xxxx)

ios - didDeselectRowAtIndexPath 仅在按下另一个单元格后调用

objective-c - Xcode 中的循环音频

ios - UITextField 上的圆角边缘

objective-c - 启用静音开关后如何在 iOS 上播放声音