您好我是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;
}
答案 0 :(得分:4)
tableview的这种行为称为tableviewcell的动态自我调整大小。设置按钮和textview的约束,如图所示。禁用textview滚动。 在 viewDidLoad()
中添加以下内容tableView.estimatedRowHeight = 44.0;
tableView.rowHeight = UITableViewAutomaticDimension;
答案 1 :(得分:0)
简而言之,您需要实现heightForRowAtIndexPath:
方法,获取模型的引用以获取文本,计算文本大小,然后相应地返回单元格的高度。
此blog将引导您完成此过程的编程方式。