UITableViewCell根据内容自动高度
来源:互联网 发布:java做场景设计的优点 编辑:程序博客网 时间:2024/05/17 07:53
先记着个属性吧,知道就行:uitableview有个属性值 separatorStyle 可用来去掉cell间的白线:
tableView.separatorStyle =UITableViewCellSeparatorStyleNone; //总共有三个值,可以试下有空
转载自:http://aclyyx.iteye.com/blog/1634726
啰嗦几句:
这里主要是通过NSString的sizeWithFont:constrainedToSize:lineBreakMode:方法实现。
该方法用于计算显示完整内容所需要的最小尺寸。
下面是摘抄的说明
NSString的函数sizeWithFont:constrainedToSize:lineBreakMode:
API文档解释:若字符串以规定的约束条件进行描绘,则返回实际的大小。
用途:返回以指定字体进行描绘时,字符串所占据的实际大小。
API文档解释:若字符串以规定的约束条件进行描绘,则返回实际的大小。
用途:返回以指定字体进行描绘时,字符串所占据的实际大小。
运行效果:
程序解析:
该部分主要摘抄自《UITableView高度自適應》,稍稍做了改动。
- - (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- static NSString * CellIdentifier = @"MessageViewControllerCell";
- int row = [indexPath row];
- UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
- if (cell == nil) {
- // 用何種字體進行顯示
- UIFont *font = [UIFont systemFontOfSize:14];
- // 該行要顯示的內容
- NSString *content = [_arr objectAtIndex:row];
- // 实例化单元格对象
- cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
- // 设置显示文字
- cell.textLabel.text = content;
- // 設置自動換行(重要)
- cell.textLabel.numberOfLines = 0;
- // 設置顯示字體(一定要和之前計算時使用字體一至)
- cell.textLabel.font = font;
- }
- return cell;
- }
- - (float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- int row = [indexPath row];
- // 列寬
- CGFloat contentWidth = self.tv.frame.size.width;
- // 用何種字體進行顯示
- UIFont *font = [UIFont systemFontOfSize:14];
- // 該行要顯示的內容
- NSString *content = [_arr objectAtIndex:row];
- // 計算出顯示完內容需要的最小尺寸
- CGSize size = [content sizeWithFont:font constrainedToSize:CGSizeMake(contentWidth, 1000.0f) lineBreakMode:UILineBreakModeWordWrap];
- // 這裏返回需要的高度
- return size.height+20;
- }
参考链接:
IOS开发之常用系统函数收集:http://fulerbakesi.iteye.com/blog/1592224
UITableView高度自適應:http://mrjeye.iteye.com/blog/1045785
- UITableViewCell根据内容自动高度
- UITableViewCell根据内容自动高度
- UITableViewCell根据内容自适应高度
- 根据UILable高度让UITableViewCell自动适应高度
- UITableViewCell根据内容显示不同的高度问题
- 根据内容自动调整IFrame高度
- iframe 的高度根据内容自动调整
- 根据内容自动调整iframe框架高度
- 让UIWebView根据内容自动调整高度
- 根据内容自动调整高度的cell
- 让iframe根据内容自动调整高度~~~
- 根据内容自动调整IFrame高度
- uilable根据内容多少自动改变高度
- UILable根据内容多少自动改变高度
- UILabel根据内容自动调整高度
- 根据内容自动调节cell的高度
- UILabel根据内容自动调整高度
- textarea根据内容自动增加高度
- ActiveMQ学习(二)——MQ的工作原理
- Gabor函数和Gabor滤波器的原理和实现
- ”预处理器定义“使用
- 谈谈javascript原型构造机制
- android strings.xml转义字符, 注意细节解决
- UITableViewCell根据内容自动高度
- C/C++微软亚洲技术中心面试题及答案
- ios各种动画效果
- pycharm the jvm could not be started错误修复
- mysql replication 互为主从的安装及配置,以及数据同步
- Ubuntu 下设置环境变量
- C++中map遍历的两种方式
- IOS设计UI工具大全
- 1.3JSP声明格式