TableView里面的cell根据图片或者文字自定义高度
2015-10-08 20:56
495 查看
1.自定义cell的.h文件里
/*声明一个类方法 ,用来计算高度 */
+ (CGFloat)heightWithText: (NSString *)text;
2.自定义.m文件里面实现
+ (CGFloat)heightWithText: (NSString *)text
{
CGRect rect = [text
boundingRectWithSize:CGSizeMake([UIScreen
mainScreen].bounds.size.width ,
0) options:(NSStringDrawingUsesLineFragmentOrigin)
attributes:@{NSFontAttributeName :[UIFont
systemFontOfSize:17]}
context:nil];
return rect.size.height;
}
3.方法调用
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath
*)indexPath
{
(Cell1为自定义cell)
return [Cell1
heightWithText:[self.arr
objectAtIndex:indexPath.row]];
}
二.图片自定义高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath
*)indexPath
{
UIImage *image = [UIImage
imageNamed:[self.arr
objectAtIndex:indexPath.row]];
return
375 * image.size.height / image.size.width;
}
/*声明一个类方法 ,用来计算高度 */
+ (CGFloat)heightWithText: (NSString *)text;
2.自定义.m文件里面实现
+ (CGFloat)heightWithText: (NSString *)text
{
CGRect rect = [text
boundingRectWithSize:CGSizeMake([UIScreen
mainScreen].bounds.size.width ,
0) options:(NSStringDrawingUsesLineFragmentOrigin)
attributes:@{NSFontAttributeName :[UIFont
systemFontOfSize:17]}
context:nil];
return rect.size.height;
}
3.方法调用
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath
*)indexPath
{
(Cell1为自定义cell)
return [Cell1
heightWithText:[self.arr
objectAtIndex:indexPath.row]];
}
二.图片自定义高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath
*)indexPath
{
UIImage *image = [UIImage
imageNamed:[self.arr
objectAtIndex:indexPath.row]];
return
375 * image.size.height / image.size.width;
}
相关文章推荐
- 获取网页的源码
- 如何分析APP功能需求、结构?
- OO原则
- hiho1088 Right-click Context Menu 递归 贪心
- 黑马程序员——Java基础语法---函数与数组
- 集合类的深入理解
- android工程转换到apk
- 字节流复制
- 一起学习android图片四舍五入图片集资源 (28)
- jsp中静态include和动态include的区别
- Android中Button的使用方法
- nyoj 434 Jungle Roads 【prim&&最小生成树】
- 【leetcode】129. Sum Root to Leaf Numbers
- mysql的collation
- Linux之V4L2基础编程
- 小小君的C语言第一课
- H桥电路
- MVC中HTML控件设为只读readonly
- java基础之内部类(四)
- POJ 1470 Closest Common Ancestors(LCA&RMQ)