IOS 开发之 cell高度自适应
来源:互联网 发布:淘宝宝贝下载阿尔法 编辑:程序博客网 时间:2024/05/21 12:49
如果此文帮助了您,请点击喜欢或评论,如果您喜欢我的文章请关注我,您的支持永远都是我前行的动力.
viewDidLoad
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
self.array = [NSArray arrayWithObjects:@"测试数据1测试数据1测试数据1测试数据1测试数据1",@"测试数据2测试数据2测试数据2测试数据2测试数据2测试数据2测试数据2",@"测试数据3测试数据3测试数据3测试数据3测试数据3测试数据3测试数据3测试数据3测试数据3测试数据3测试数据3测试数据3测试数据3测试数据3测试数据3测试数据3测试数据3测试数据3",@"测试数据测试数据测试数测试数据测试数据",@"测试数据4测试数据4",nil];
[self.tableView registerClass:[TableViewCell class] forCellReuseIdentifier:identifier];
}
cellForRow
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
cell.titleLable.text = _array[indexPath.row];
1.计算文本高度 CGFloat height = [self textHeight:_array[indexPath.row]];
2.改变lable的高度
cell.titleLable.frame = CGRectMake(10, 10, 394, height);
return cell;
}
heightForRow
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
height会比cellForRow先执行,所以我们需要先计算好cell的高度.
1.先获取当前行所对应的字符串
NSString *string = _array[indexPath.row];
2.计算字符串的高度 CGFloat height = [self textHeight:string];
3.返回计算好的高度
return height + 20; // 加上空白区域的高度
}
textHeight:(NSString *)string
自定义高度
-(CGFloat)textHeight:(NSString *)string{
传字符串返回高度
计算字符串所占的矩形区域的大小 CGRect rect =[string boundingRectWithSize:CGSizeMake(394, 9999) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17]} context:nil];
返回高度 return rect.size.height;
}
转载请注明出处.
- IOS 开发之 cell高度自适应
- iOS 之cell自适应高度
- iOS开发中cell高度的自适应
- 【iOS开发】UITableView Cell 自适应高度
- iOS Cell高度自适应
- ios cell自适应高度
- iOS cell 自适应高度
- (0072)iOS开发之UITableViewCell高度自适应探索--cell预估高度
- iOS开发之UITableview之多种Cell高度自适应实现方案的UI流畅度分析
- iOS 自定义Cell 自适应高度
- iOS 8 自适应 Cell高度
- IOS--UI Cell 自适应高度
- iOS实现Cell自适应高度
- iOS开发之多种Cell高度自适应实现方案的UI流畅度分析
- iOS开发之多种Cell高度自适应实现方案的UI流畅度分析
- iOS开发继iOS8后UITableView的Cell高度自适应
- iOS 开发--相对来说比较简单的cell高度自适应
- iOS cell高度自适应_动态调节cell高度
- 对线性回归,logistic回归和一般回归的认识
- hdoj 2108 Shape of HDU 【向量叉乘判是否共线】
- 微信开发笔记——微信网页登录授权,获取用户信息
- c++第一次作业
- HTTP协议之ContentType
- IOS 开发之 cell高度自适应
- Floating Action Button-Android M新控件
- TabLayout和ViewPager的简单联用
- 在对结构体分配的内存进行free时出现,关于 Windows 已在 **.exe 中触发一个断点。其原因可能是堆被损坏.
- hdu2795 线段树 贴广告
- BZOJ 8843 染色 [树链剖分+区间线段树]
- 随机步法
- 【MVC框架系列】(一)——Struts,让我把你看得明明白白
- Linux scp 命令