动态设置UITableViewCell的高度
2015-07-13 11:27
405 查看
目的:计算出图中红色部分的实际高度,根据字体大小、字型、内容长短自动算出UILabel的高度。
![](https://img-blog.csdn.net/20150713113801943?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
代码如下:
- (CGFloat)tableView:(__unused UITableView *)tableView heightForRowAtIndexPath:(__unused NSIndexPath *)indexPath
{
//除了content之外的其他高度总和,这个你可以从布局文件中得到(上图中的红色部分除外的其他部分高的总和)
float cellHeight = 55.0f;
//获取Cell
AlarmAlertCell *cell = (AlarmAlertCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath];
//获取数据源
AlarmNSString *data = [_dataSource objectAtIndex:indexPath.row];
//获取高度
CGSize size = [data.content sizeWithFont:cell.message.font constrainedToSize:CGSizeMake(cell.message.frame.size.width, MAXFLOAT) lineBreakMode:NSLineBreakByWordWrapping];
//得到当前字体所显示的行数
int cellRow = size.height/14;
//通过行数算出content所占的实际高度
cellHeight = cellHeight + (14+7)*cellRow;
NSLog(@"cellHeight = %f", cellHeight);
return cellHeight;
}
代码如下:
- (CGFloat)tableView:(__unused UITableView *)tableView heightForRowAtIndexPath:(__unused NSIndexPath *)indexPath
{
//除了content之外的其他高度总和,这个你可以从布局文件中得到(上图中的红色部分除外的其他部分高的总和)
float cellHeight = 55.0f;
//获取Cell
AlarmAlertCell *cell = (AlarmAlertCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath];
//获取数据源
AlarmNSString *data = [_dataSource objectAtIndex:indexPath.row];
//获取高度
CGSize size = [data.content sizeWithFont:cell.message.font constrainedToSize:CGSizeMake(cell.message.frame.size.width, MAXFLOAT) lineBreakMode:NSLineBreakByWordWrapping];
//得到当前字体所显示的行数
int cellRow = size.height/14;
//通过行数算出content所占的实际高度
cellHeight = cellHeight + (14+7)*cellRow;
NSLog(@"cellHeight = %f", cellHeight);
return cellHeight;
}
相关文章推荐
- mysql中sql语句slow query
- IOS - UITextField输入长度限制
- UITableVIewCell的设置(附标题/高度/右边的标识)
- Android Bluetooth Low Energy(Android低功耗蓝牙)
- UIAlertController
- Unique Paths
- goquery 添加header 发起请求
- Android的线程使用来更新UI----Thread、Handler、Looper、TimerTask等
- 【Java】用两个栈实现一个队列Myqueue类
- iOS开发UI篇 -- UICollectionView
- UITableView显示不全
- iOS UITableviewCell的删除
- ios UISearchDisplayController 实现 UITableView 搜索功能
- nested exception is org.hibernate.QueryException:could not resolve property:
- request、response 中文乱码问题与解决方式
- Unique Paths
- HDU 1503 Advanced Fruits (LCS,变形)
- iOS开发UI篇 -- UITableView
- VS 2013使用SOUI时(vcpkgsrv.exe)进程导致物理内存升高解决的办法
- [Leetcode 232, Easy] Implement Queue using Stacks