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
Sunnyyoung
V2EX  ›  iDev

[请教]如何利用 AutoLayout 实现 Collapable TableViewCell

  •  
  •   Sunnyyoung · 2015-09-06 09:13:17 +08:00 · 3343 次点击
    这是一个创建于 3168 天前的主题,其中的信息可能已经有所发展或是发生改变。

    如何利用 AutoLayout 实现 Collapable TableViewCell 的效果呢,大家来分享下经验~

    同时想问下在使用了 AutoLayout 的情况下,如何比较智能自动地计算 UITableViewCell 的高度?据我所了解的有以下几种:

    但是问题来了,如果我使用的是 UITableView-FDTemplateLayoutCell , Cell 里面的元素的高度是需要动态改变的(例如九宫格相册显示),那需要怎么配合使用呢?目前我的方法是根据图片的数量来设置该 View 的 HeightConstraint , ReloadData ,但是有时候会有 AutoLayout 的错误 Log = =

    请大家指教指教

    14 条回复    2015-09-07 14:39:23 +08:00
    a412739861
        1
    a412739861  
       2015-09-06 10:52:19 +08:00
    自动计算行高?
    如果有 autolayout 了。
    在 iOS8 下, heightForRowAtIndexPath 下使用 UITableViewAutomaticDimension ,就好了吧,我自己的是这么做的……不过没图,只是文字。
    iOS7 下面,取一个不显示在界面上的 cell ,把内容设置进去,然后 cellcontentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height + 1 就好了。
    不过图片什么的我就不知道了……
    xi_lin
        2
    xi_lin  
       2015-09-06 12:31:04 +08:00
    我现在也在用 UITableView-FDTemplateLayoutCell
    9 宫格我是用了`UICollectionView`同时根据数量定高算
    collapable 也是改变高度约束然后 reloadData

    你看到的错误 log 是什么内容?
    Sunnyyoung
        3
    Sunnyyoung  
    OP
       2015-09-06 14:08:25 +08:00
    @xi_lin 我也是跟你一样诶,根据图片的数量来计算行数,从而得到 CollectionView 的高度,设置其 Height Constraint ,但是滚动的时候,有些 TableViewCell 的 CollectionView 的 Height Constraint 会报错。。但是计算没有错误阿,想不通= =

    @a412739861 因为要兼容 iOS7 及其以上,感觉用 UITableView-FDTemplateLayoutCell 这个是比较好的选择,但是有图片,而且要根据图片数量来改变高度,就有点麻烦了
    a412739861
        4
    a412739861  
       2015-09-06 16:07:53 +08:00
    @Sunnyyoung iOS7 就是我下面说的那个,不过直接复用第三方的就用呗。如果直接设置其他图片的高度为 0 呢
    HappyHacking
        5
    HappyHacking  
       2015-09-06 16:20:01 +08:00
    楼主是想实现类似微信朋友圈那种样子吗?
    我没实现过,不过我帮你看了一下微信朋友圈的 UI 结构:
    收缩起来的:
    http://i3.tietuku.com/ca794ab2d1d7e53d.png

    展开来的:
    http://i3.tietuku.com/c36ad0a73a62dfc7.png
    Sunnyyoung
        6
    Sunnyyoung  
    OP
       2015-09-06 16:24:57 +08:00
    @HappyHacking 嗯,我也用 Reveal 看过,但是微信的布局没有用到 AutoLayout ,而我想知道用 AutoLayout 怎么实现比较好,毕竟现在 AutoLayout 已经算是一个趋势了吧~
    HappyHacking
        7
    HappyHacking  
       2015-09-06 16:31:16 +08:00
    @Sunnyyoung 你怎么知道没用 AutoLayout 呢?哪里可以看出来?
    Sunnyyoung
        8
    Sunnyyoung  
    OP
       2015-09-06 16:50:51 +08:00
    @HappyHacking 如果使用了 AutoLayout , Reveal 是可以看到对应 Constraint 的
    HappyHacking
        9
    HappyHacking  
       2015-09-06 17:49:15 +08:00
    搜噶, Thanks
    xi_lin
        10
    xi_lin  
       2015-09-06 19:19:02 +08:00
    @Sunnyyoung 你看看报出来的错是不是真的有不能满足的约束呗。推荐用 Masonry ,报错也更可读一点。
    Sunnyyoung
        11
    Sunnyyoung  
    OP
       2015-09-07 10:31:55 +08:00
    @xi_lin 想请问下你是在哪个地方更新 View 的 HeightConstraint 的呢
    xi_lin
        12
    xi_lin  
       2015-09-07 10:57:24 +08:00
    @Sunnyyoung `tableView:cellForRowAtIndexPath:`取出 cell 以后更新
    Sunnyyoung
        13
    Sunnyyoung  
    OP
       2015-09-07 12:11:52 +08:00
    @xi_lin 奇了个大怪了,我也是这样的,但是还是报错,计算没问题的阿
    xi_lin
        14
    xi_lin  
       2015-09-07 14:39:23 +08:00
    @Sunnyyoung 你加大两倍 height 看看能不能消除错误。如果能那就是你计算有问题。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3817 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 10:37 · PVG 18:37 · LAX 03:37 · JFK 06:37
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.