我有UITableView
。在每个单元格中,我都有UITextView
,长度可变。
我希望UITextView
扩展到适合所有文本。
[UITextView sizeToFit];
不起作用。 此外,我需要它的扩展向下移动到单元格中的其他标签和对象(当然,单元格的高度必须改变)。 我该怎么做?
答案 0 :(得分:1)
首先,您必须声明委托方法heightForRowAtIndexPath:
,然后使用此代码
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
CGFloat rowHeight;
NSString *text1 = [arrTemp objectAtIndex:indexPath.row]; //your text
CGRect frame = [text1 boundingRectWithSize:CGSizeMake(160, CGFLOAT_MAX)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:@{NSFontAttributeName:[UIFont fontWithName:@"Helvetica" size:12]}
context:nil]; // here get the height of ur textline
CGSize size = CGSizeMake(frame.size.width, frame.size.height+1);
rowHeight=size.height*2;
return rowHeight;
}
希望这段代码可能对您非常有帮助,您可以轻松实现它。
答案 1 :(得分:0)
您需要根据uitextview的框架更新每个视图的框架
这是我想说的,它只是你修改了所有下一个视图的框架的样本
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"tblCell"];
UITextView *txt = [cell viewWithTag:1];
txt.text =[arr objectAtIndex:indexPath.row];
[txt sizeToFit];
CGRect frame = txt.frame;
CGRect frame2;
UILabel *lbl = [cell viewWithTag:2];
frame2 = lbl.frame;
if(frame.size.width + frame2.size.width >= cell.frame.size.width){
frame2.origin.x = 0;
frame2.origin.y += frame.size.height;
[lbl setFrame:frame2];
}
else{
frame2.origin.x = frame.size .width;
[lbl setFrame:frame2];
}
return cell;
}