ios tableViewcell 的自适应高度
2016-02-25 11:31
330 查看
在做tableView的时候,我们有时候需要根据cell的高度动态来调整,最近在网上看到一段代码不错,跟大家Share一下。
在
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
类中获取cell的高度:
[objc] view plain copy
CGSize boundSize = CGSizeMake(216, CGFLOAT_MAX);
cell.textLabel.text = @"12345678900123456789";
cell.userInteractionEnabled = NO;
cell.textLabel.numberOfLines = 0;
CGSize requiredSize = [cell.textLabel.text sizeWithFont:[UIFont systemFontOfSize:13] constrainedToSize:boundSize lineBreakMode:UILineBreakModeWordWrap];
CGRect rect = cell.frame;
rect.size.height = requiredSize.height+5;
cell.frame = rect;
这时候获取到了cell的高度,然后在
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
类中改变cell的高度:
[objc] view plain copy
UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
NSLog(@"cell height %f",cell.frame.size.height);
return cell.frame.size.height;
这样以来cell的高度就根据cell里label的内容自动改变啦。
其主要出发点就是我有一个label,然后我要把这个label展示出来,我根据字体的大小还有行数来获取一个高度,这样cell的高度就有啦
在
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
类中获取cell的高度:
[objc] view plain copy
CGSize boundSize = CGSizeMake(216, CGFLOAT_MAX);
cell.textLabel.text = @"12345678900123456789";
cell.userInteractionEnabled = NO;
cell.textLabel.numberOfLines = 0;
CGSize requiredSize = [cell.textLabel.text sizeWithFont:[UIFont systemFontOfSize:13] constrainedToSize:boundSize lineBreakMode:UILineBreakModeWordWrap];
CGRect rect = cell.frame;
rect.size.height = requiredSize.height+5;
cell.frame = rect;
这时候获取到了cell的高度,然后在
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
类中改变cell的高度:
[objc] view plain copy
UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
NSLog(@"cell height %f",cell.frame.size.height);
return cell.frame.size.height;
这样以来cell的高度就根据cell里label的内容自动改变啦。
其主要出发点就是我有一个label,然后我要把这个label展示出来,我根据字体的大小还有行数来获取一个高度,这样cell的高度就有啦
相关文章推荐
- ios 判断电话号码
- iOS获取设备信息与应用信息
- iOS block教程
- iOS分组通讯录效果+侧滑菜单(MMDrawerController)
- iOS 开启个人热点后屏幕的适配问题
- nagios
- IOS异步获取数据并刷新界面dispatch_async的使用方法
- iOS 提交错误
- iOS Crash日志
- iOS 让启动页面延迟的方法
- iOS开发-调试(debug)
- iOS中的谓词(NSPredicate)使用
- iOS数据持久化
- iOS开发---如何给类别增加属性
- iOS开发xib控件删不掉,修改xib运行不发生改变,修改xib不管用
- iOS基础动画教程
- iOS 上线 LaunchImage、Icon、iTunes connect的屏幕快照——iOS 上线相关(一)
- iOS开发笔记--iOS图解多线程
- iOS获取设备电量、内存、容量
- 初学者要了解的第三方库