
*预设高度
((UITableView)view)estimatedRowHeight = 90; //这个数值越接近预期高度越好
((UITableView)view)rowHeight = UITableViewAutomaticDimension;
要预设高度 cell中会变frame的子view必须要有(你这里就是UITextView了) 相对于cell底部的约束
有时我们需要动态调整UITableViewCell的高度,根据内容的不同设置不同的高度,以前看到一种实现方法,写得有点麻烦,具体地址找不到了,这里有个更好的(至少我认为),分享一下部分代码。 - (UITableViewCell )tableView:(UITableView )table
这种方法一般用于tableview,制作类似于微博浏览,空间展示动态的功能。表的高度根据以及字符串字数确定。
通过文字展示的最大宽度以及文字字体大小确定文字展示的高度
[objc] view plain copy
- (CGRect)getHeightOfText:(NSString )text width:(CGFloat)width font:(UIFont )font{
/
width:设定的字符串展示的宽度
font :字符的字体大小
/
CGRect rect = [text boundingRectWithSize:CGSizeMake(width, 0) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:font} context:nil];
/
字符串对应高度
float aHeifht = rectsizeheight;
字符串对应宽度
float aWidth = rectsizewidth;
/
return rect;
}
根据需要展示的高度或者文字高度自适应表行高
[objc] view plain copy
- (CGFloat)tableView:(UITableView )tableView heightForRowAtIndexPath:(NSIndexPath )indexPath{
if (文字内容){
NSString text = [NSString stringWithFormat:@"\t%@",需要展示的文字];
CGRect rect = [self getHeightOfText:text width:300 font:[UIFont systemFontOfSize:14]];
return rectsizeheight+10;
}else//展示
{
return [[ objectForKey:@"height"] integerValue]+10;//获取服务器中高度,为了美观都比原始高多10
}
}
设置cell内容
[objc] view plain copy
- (UITableViewCell )tableView:(UITableView )tableView cellForRowAtIndexPath:(NSIndexPath )indexPath{
NewsDetailCell cell = [tableView dequeueReusableCellWithIdentifier:@"data"];
NSDictionary content = [_dataArray objectAtIndex:indexPathrow];
if ([[content objectForKey:@"data_type"] intValue] == 1) {
//文本
NSString text = [NSString stringWithFormat:@"\t%@",[content objectForKey:@"content"]];
celltxtLabeltext = text;
CGRect rect = [CommonTool getHeightOfText:text width:300 font:[UIFont systemFontOfSize:14]];
celltxtLabelframe = CGRectMake(10, 5, rectsizewidth, rectsizeheight);
}else{
//
NSString urlString = [NSString stringWithFormat:@"%@%@",SERVER_ADDRESS,[[content objectForKey:@"image"] objectForKey:@"source"]];
[cellnewsImageView setImageWithURL:[NSURL URLWithString:urlString]];
cellnewsImageViewframe = CGRectMake(10, 5, 300, [[[content objectForKey:@"image"] objectForKey:@"height"] integerValue]);
}
return cell;
}
1、新建一个基于singleview的工程,然后删除默认Storyboard的ViewController,拖拽一个TableviewController,设置为inital Controller
2、往Prototype Cells上拖拽两个UILabel
如图
3、为两个Label设置属性
Title
设置tag为10
4、Detail
设置tag为11
5、为两个Label设置AutoLayout
Title
注意,这里把title放在左上角,Detail放在左下角。然后添加二者之间的距离恒定为1,那么AutoLayout就会自动计算出高度。
新建一个TableviewController,并且讲storyboard上的tableviewController设置为新建的类
设置Tableview的高度为自动获取
-(CGFloat)tableView:(UITableView )tableView estimatedHeightForRowAtIndexPath:(NSIndexPath )indexPath{
return UITableViewAutomaticDimension;
}
-(CGFloat)tableView:(UITableView )tableView heightForRowAtIndexPath:(NSIndexPath )indexPath{
return UITableViewAutomaticDimension;
}
加入存储数据的数组,并且在初始化里设定数据
@property (strong,nonatomic)NSArray titleArray;
@property (strong,nonatomic)NSArray detailArray;
- (void)viewDidLoad {
[super viewDidLoad];
selftitleArray = @[@"1",@"2",@"3"];
selfdetailArray = @[@"shot",@"Aduahguhauhguhaudghuahguhudhauhg",@"dhuahgudhaughuahdughuahguhauhguhdahudhuahughduahguhadguhaduhguadhughduahguahguhadugh"];
}
接下来就是Tablview的常用的,很好理解,这里不多赘述
- (NSInteger)numberOfSectionsInTableView:(UITableView )tableView {
return 1;
}
- (NSInteger)tableView:(UITableView )tableView numberOfRowsInSection:(NSInteger)section {
return selftitleArraycount;
}
-(BOOL)prefersStatusBarHidden{
return true;
}
- (UITableViewCell )tableView:(UITableView )tableView cellForRowAtIndexPath:(NSIndexPath )indexPath {
UITableViewCell cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
UILabel titleLabel = (UILabel )[cell viewWithTag:10];
UILabel contentLabel = (UILabel )[cell viewWithTag:11];
titleLabeltext = selftitleArray[indexPathrow];
contentLabeltext = selfdetailArray[indexPathrow];
contentLabelnumberOfLines = 0;
return cell;
}
然后,就得到了我们想要的效果了。
以上就是关于如何精确适配放置 UITextView 的 tableView Cell 的高度全部的内容,包括:如何精确适配放置 UITextView 的 tableView Cell 的高度、UITableView中的Cell怎么在运行期间更改高度、如何根据字数多少获取字符串对应高度等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)