Ios 如何根据UIfields内容大小增加tableview单元格高度

Ios 如何根据UIfields内容大小增加tableview单元格高度,ios,objective-c,Ios,Objective C,您好,我是iOS新手,在我的项目中,我在UITableViewCell上添加了两个UIButtons和一个UITextView,这里的UITextView根据内容大小不断增长,因为数据来自服务 我的要求是基于textSize-为此必须增加单元格高度。我已经写了一些代码,但不起作用。我做错了什么?请帮助我,我已经尝试了很久,但我没有得到结果 我的代码:- -(void)viewDidLoad{ [超级视图下载]; tableList=[[UITableView alloc]init]; table

您好,我是iOS新手,在我的项目中,我在
UITableViewCell
上添加了两个
UIButton
s和一个
UITextView
,这里的
UITextView
根据内容大小不断增长,因为数据来自服务

我的要求是基于
textSize
-为此必须增加单元格高度。我已经写了一些代码,但不起作用。我做错了什么?请帮助我,我已经尝试了很久,但我没有得到结果

我的代码:-
-(void)viewDidLoad{
[超级视图下载];
tableList=[[UITableView alloc]init];
tableList.TranslatesAutoResizezingMaskintoConstraints=否;
tableList.tableFooterView=[[UIView alloc]initWithFrame:CGRectZero];
tableList.dataSource=self;
tableList.delegate=self;
tableList.autoresizingMask=UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
[tableList注册表类:[UITableViewCell类]强制重用标识符:@“Cell”];
[self.view addSubview:tableList];
NSDictionary*视图=NSDictionaryOfVariableBindings(tableList);
NSArray*水平约束=[NSLayoutConstraintsWithVisualFormat:@“H:|-0-[tableList]-0-|”选项:0度量:无视图:视图];
NSArray*垂直约束=[NSLayoutConstraint Constraints With VisualFormat:@“V:|-0-[tableList]-0-|”选项:0度量:无视图:视图];
[self.view addconstraint:水平约束];
[self.view addConstraints:verticalConstraint];
}
-(NSInteger)表格视图中的节数:(UITableView*)表格视图{
返回1;
}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节{
返回10;
}
-(CGFloat)tableView:(UITableView*)表视图行高度索引路径:(NSIndexPath*)索引路径{
返回UITableViewAutomaticDimension
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
静态NSString*cellIdentifier=@“Cell”;
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
Cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault重用标识符:cellIdentifier];
UITextView*aboutText=[[UITextView alloc]init];
aboutText.font=[UIFont fontWithName:@“常规”大小:15.0f];
aboutText.translatesAutoresizingMaskIntoConstraints=否;
aboutText.backgroundColor=[UIColor darkGrayColor];
aboutText.scrollEnabled=否;
[Cell.contentView addSubview:aboutText];
UIButton*button1=[[UIButton alloc]init];
按钮1.Translates自动调整大小GMaskintoConstraints=否;
button1.backgroundColor=[UIColor redColor];
[Cell.contentView addSubview:button1];
UIButton*button2=[[UIButton alloc]init];
按钮2.translates自动调整大小GMaskintoConstraints=否;
按钮2.backgroundColor=[UIColor orangeColor];
[Cell.contentView addSubview:button2];
NSDictionary*视图=NSDictionaryOfVariableBindings(关于文本,按钮1,按钮2);
NSArray*formulaH=[NSLayoutConstraint Constraints with VisualFormat:@“H:|-10-[aboutText]-10-|”
选项:0
指标:零
意见:意见];
NSArray*button1H=[NSLayoutConstraint Constraints with VisualFormat:@“H:|-10-[button1]-10-|”
选项:0
指标:零
意见:意见];
NSArray*button2H=[NSLayoutConstraint Constraints with VisualFormat:@“H:|-10-[button2]-10-|”
选项:0
指标:零
意见:意见];
NSArray*垂直间距=[NSLayoutConstraint Constraints with VisualFormat:@“V:|-5-[aboutText]-10-[button1(30)]-10-[button2(30)]”
选项:0
指标:零
意见:意见];
[Cell.contentView addConstraints:formulaH];
[Cell.contentView addConstraints:button1H];
[Cell.contentView addConstraints:button2H];
[Cell.contentView添加约束:垂直间距];
aboutText.text=@"在UIViewAlertForUnsatifiableConstraints处创建一个符号断点,以便在调试器中捕获此内容。中列出的UIView上UIConstraintBasedLayoutDebugging类别中的方法可能也会有所帮助。在UIViewAlertForUnsatifiableConstraints处创建一个符号断点,以便在调试器中捕获此内容。UIConstraintBasedLayoutDebuggi中的方法中列出的UIView上的ng类别也可能有帮助。“;
返回单元;
}

简而言之,您需要实现
heightForRowAtIndexPath:
方法,获取模型引用以获取文本,计算文本大小,然后相应地返回单元格高度


这将引导您完成此操作的编程方式。

简而言之,您需要实现
heightforrowatinexpath:
方法,获取模型引用以获取文本,计算文本大小,然后相应地返回单元格高度


这将引导您通过编程方式完成此操作。

tableview的这种行为称为tableviewcell的动态自调整大小。设置按钮和textview的约束,如图所示。禁用textview滚动。 在- (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.estimatedRowHeight = 44.0;
tableView.rowHeight = UITableViewAutomaticDimension;