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 · 4245 次点击
    这是一个创建于 3298 天前的主题,其中的信息可能已经有所发展或是发生改变。
    比如现在有两种 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,这个不敢确定
    Knights
        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   ·   我们的愿景   ·   实用小工具   ·   2262 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 06:06 · PVG 14:06 · LAX 23:06 · JFK 02:06
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.