请教各位是如何根据 UILabel 字数的长度来自动更新多种 UITableViewCell 情况下 cell 的高度 ? - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
iOS 开发实用技术导航
NSHipster 中文版
http://nshipster.cn/
cocos2d 开源 2D 游戏引擎
http://www.cocos2d-iphone.org/
CocoaPods
http://cocoapods.org/
Google Analytics for Mobile 统计解决方案
http://code.google.com/mobile/analytics/
WWDC
https://developer.apple.com/wwdc/
Design Guides and Resources
https://developer.apple.com/design/
Transcripts of WWDC sessions
http://asciiwwdc.com
Cocoa with Love
http://cocoawithlove.com/
Cocoa Dev Central
http://cocoadevcentral.com/
NSHipster
http://nshipster.com/
Style Guides
Google Objective-C Style Guide
NYTimes Objective-C Style Guide
Useful Tools and Services
Charles Web Debugging Proxy
Smore
SeanChense
V2EX    iDev

请教各位是如何根据 UILabel 字数的长度来自动更新多种 UITableViewCell 情况下 cell 的高度 ?

  •  
  •   SeanChense 2015-04-22 17:07:45 +08:00 4835 次点击
    这是一个创建于 3828 天前的主题,其中的信息可能已经有所发展或是发生改变。
    比如现在有两种 cell 需要自动计算高度来容纳 UILabel,大家是怎么做的呢?
    有没有什么比较流行的做法?比如根据一行能容纳多少字算有多少行来更新高度,不知道这个是不是很常见。
    24 条回复    2015-05-10 20:29:01 +08:00
    holy_sin
        2
    holy_sin  
       2015-04-22 17:46:08 +08:00   1
    tableView.estimatedRowHeight = tableView.rowHeight
    tableView.rowHeight = UITableViewAutomaticDimension

    >= ios7
    holy_sin
        3
    holy_sin  
       2015-04-22 17:46:51 +08:00
    可能需要autolayout,这个不敢确定
    Knighs
        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;
    SeanChense
        5
    SeanChense  
    OP
       2015-04-22 18:46:57 +08:00
    @Knights 没看懂
    Knights
        6
    Knights  
       2015-04-22 18:52:42 +08:00
    @SeanChense 在cell里直接用autolayout是更省事的办法。我的这个是根据text字符串的长度、字体计算出cell的高度,width是屏幕的宽度。
    SeanChense
        7
    SeanChense  
    OP
       2015-04-22 18:53:22 +08:00
    @Knights 从一楼给出的链接来看,AutoLayout 一点都不省事
    Knights
        8
    Knights  
       2015-04-22 18:56:31 +08:00
    @SeanChense 很省事啊,xib里直接拖
    SeanChense
        9
    SeanChense  
    OP
       2015-04-22 22:07:05 +08:00
    @Knights 我是用 Masonry 手写的约束
    l12ab
        10
    l12ab  
       2015-04-22 22:25:23 +08:00
    我的都是纯代码,加载cell前计算label高度,然后算出cell高度,存到数组里。这种做法应该不可取
    SeanChense
        11
    SeanChense  
    OP
       2015-04-22 22:28:04 +08:00
    @l12ab 那大家标准的做法是怎么做的?
    1# 给出的做法感觉复杂好多好多
    babyname
        12
    babyname  
       2015-04-22 22:36:50 +08:00 via iPhone
    ios7直接autolayout
    max0ne
        13
    max0ne  
       2015-04-22 23:03:20 +08:00
    有一种很土的方法是用 [UILabel cellThatFits:CGRectMake(width , MAX_FLOAT)].height 得到label的高度
    SeanChense
        14
    SeanChense  
    OP
       2015-04-22 23:13:32 +08:00 via iPhone
    @babyname 我对 AutoLayout 操作无能,总是不生效,内容挤成一团
    kukat
        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还是很好用的,多看看文档多读读代码很快就能上手的。
    icodesign
        16
    icodesign  
       2015-04-23 00:00:39 +08:00
    一楼给出的回答中 iOS8 是标准做法,你可以 Masonry + AutoLayout 写在 cell 里面, iOS7 的实现的话反正是要算的,至于怎么算就各显神通吧。。还是 IOS8方便。- -!
    expkzb
        17
    expkzb  
       2015-04-23 09:26:29 +08:00
    不用autolayout坑会很多,在所有项目中我都用PureLayout(以前叫 UIView+Autolayout)

    如果觉得那套自适应的流程麻烦,现在有https://github.com/forkingdog/UITableView-FDTemplateLayoutCell

    重点是,UILabel要设定 preferredMaxLayoutWidth ,这样才能算出高度
    expkzb
        18
    expkzb  
       2015-04-23 09:29:48 +08:00
    如果只支持iOS8以上就不用那么麻烦了,写完约束,定一个 estimatedRowHeight 就可以了
    lawder
        19
    lawder  
       2015-04-24 13:23:43 +08:00
    @expkzb 如果我的tableView是跟屏幕一样宽的,UILabel设定 preferredMaxLayoutWidth时也根据屏幕宽度来设置吗?
    expkzb
        20
    expkzb  
       2015-04-24 21:26:23 +08:00   1
    @lawder 如果你的label和两边有间距,那就是 屏幕宽度 - 间距
    PhilCai
        21
    PhilCai  
       2015-04-24 23:12:26 +08:00 via iPhone
    我是用Masonry手写cell的子类约束
    SeanChense
        22
    SeanChense  
    OP
       2015-04-25 09:20:40 +08:00
    @PhilCai 我是 cell 的 contentView 中有一个需要动态变化的 label ,label 下面又有一个 UIView ,UIView 里又有一个 label 需要变化
    - | contentView
    - - | UILabel
    - - | UIView
    - - -| UILabel
    philcn
        23
    philcn  
       2015-05-10 20:10:03 +08:00
    我和同事写的一个 minimal 的 tableview cell 自动算高扩展,支持 iOS 7+,以后会引入高度缓存和预计算。https://github.com/forkingdog/UITableView-FDTemplateLayoutCell
    SeanChense
        24
    SeanChense  
    OP
       2015-05-10 20:29:01 +08:00
    @philcn 啊哈哈 事实上我已经在用了。真棒
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     2621 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 22ms UTC 15:23 PVG 23:23 LAX 08:23 JFK 11:23
    Do have faith in what you're doing.
    ubao snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86