![]() | 1 expkzb 2015-04-22 17:41:28 +08:00 ![]() |
![]() | 2 holy_sin 2015-04-22 17:46:08 +08:00 ![]() tableView.estimatedRowHeight = tableView.rowHeight tableView.rowHeight = UITableViewAutomaticDimension >= ios7 |
![]() | 3 holy_sin 2015-04-22 17:46:51 +08:00 可能需要autolayout,这个不敢确定 |
![]() | 4 Knights 2015-04-22 18:44:44 +08:00 NSDictionary *attributes = @{NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue" size:14]}; // NSString class method: boundingRectWithSize:options:attributes:context is // available only on ios7.0 sdk. CGRect rect = [text boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil]; CGFloat height = MAX(rect.size.height, 95.0); return height; |
![]() | 5 SeanChense OP @Knights 没看懂 |
![]() | 6 Knights 2015-04-22 18:52:42 +08:00 @SeanChense 在cell里直接用autolayout是更省事的办法。我的这个是根据text字符串的长度、字体计算出cell的高度,width是屏幕的宽度。 |
![]() | 7 SeanChense OP @Knights 从一楼给出的链接来看,AutoLayout 一点都不省事 |
![]() | 8 Knights 2015-04-22 18:56:31 +08:00 @SeanChense 很省事啊,xib里直接拖 |
![]() | 9 SeanChense OP @Knights 我是用 Masonry 手写的约束 |
10 l12ab 2015-04-22 22:25:23 +08:00 我的都是纯代码,加载cell前计算label高度,然后算出cell高度,存到数组里。这种做法应该不可取 |
![]() | 11 SeanChense OP @l12ab 那大家标准的做法是怎么做的? 1# 给出的做法感觉复杂好多好多 |
![]() | 12 babyname 2015-04-22 22:36:50 +08:00 via iPhone ios7直接autolayout |
![]() | 13 max0ne 2015-04-22 23:03:20 +08:00 有一种很土的方法是用 [UILabel cellThatFits:CGRectMake(width , MAX_FLOAT)].height 得到label的高度 |
![]() | 14 SeanChense OP @babyname 我对 AutoLayout 操作无能,总是不生效,内容挤成一团 |
![]() | 15 kukat 2015-04-22 23:57:19 +08:00 终极方案 http://stackoverflow.com/questions/18746929/using-auto-layout-in-uitableview-for-dynamic-cell-layouts-variable-row-heights/18746930#18746930 另外,不要因为不会用AutoLayout而否定他,做动态布局特别是动态Cell高度的时候AutoLayout还是很好用的,多看看文档多读读代码很快就能上手的。 |
![]() | 16 icodesign 2015-04-23 00:00:39 +08:00 一楼给出的回答中 iOS8 是标准做法,你可以 Masonry + AutoLayout 写在 cell 里面, iOS7 的实现的话反正是要算的,至于怎么算就各显神通吧。。还是 IOS8方便。- -! |
![]() | 17 expkzb 2015-04-23 09:26:29 +08:00 不用autolayout坑会很多,在所有项目中我都用PureLayout(以前叫 UIView+Autolayout) 如果觉得那套自适应的流程麻烦,现在有https://github.com/forkingdog/UITableView-FDTemplateLayoutCell 重点是,UILabel要设定 preferredMaxLayoutWidth ,这样才能算出高度 |
![]() | 18 expkzb 2015-04-23 09:29:48 +08:00 如果只支持iOS8以上就不用那么麻烦了,写完约束,定一个 estimatedRowHeight 就可以了 |
19 lawder 2015-04-24 13:23:43 +08:00 @expkzb 如果我的tableView是跟屏幕一样宽的,UILabel设定 preferredMaxLayoutWidth时也根据屏幕宽度来设置吗? |
![]() | 21 PhilCai 2015-04-24 23:12:26 +08:00 via iPhone 我是用Masonry手写cell的子类约束 |
![]() | 22 SeanChense OP @PhilCai 我是 cell 的 contentView 中有一个需要动态变化的 label ,label 下面又有一个 UIView ,UIView 里又有一个 label 需要变化 - | contentView - - | UILabel - - | UIView - - -| UILabel |
![]() | 23 philcn 2015-05-10 20:10:03 +08:00 我和同事写的一个 minimal 的 tableview cell 自动算高扩展,支持 iOS 7+,以后会引入高度缓存和预计算。https://github.com/forkingdog/UITableView-FDTemplateLayoutCell |
![]() | 24 SeanChense OP @philcn 啊哈哈 事实上我已经在用了。真棒 |