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