DGTemplateLayoutCell 1.1.2

DGTemplateLayoutCell 1.1.2

测试已测试
Lang语言 SwiftSwift
许可证 MIT
Released最新版本2016 年 8 月
SPM支持 SPM

zhaodg 维护。



  • 作者:
  • zhaodg

DGTemplateLayoutCell (Swift 3.0)

用于自动计算 UITableViewCell 高度的模板自动布局单元格。(Swift 3.0

DGTemplateLayoutCell 是对 forkingdog 的 UITableView-FDTemplateLayoutCell 的复制和修改,并在 Swift 3.0 中实现。

需求

  • Xcode 7 或更高版本
  • iOS 8.0 或更高版本(可能在旧版本上也可用,但未进行测试)
  • ARC
  • Swift 3.0

概览

用于 自动 计算 UITableViewCell 高度的模板自动布局单元格。

Demo Overview

基本用法

如果您有一个 自满足的 单元单元格,那么您只需做以下事情

import DGTemplateLayoutCell

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return tableView.dg_heightForCellWithIdentifier("DGFeedCell", configuration: { (cell) -> Void in
    // Configure this cell with data, same as what you've done in "-tableView:cellForRowAtIndexPath:"
    // Like:
    //    let cell = cell as! DGFeedCell
    //    cell.loadData(self.feedList[indexPath.section][indexPath.row])
    })
}

高度缓存 API

自 iOS8 以来,`-tableView:heightForRowAtIndexPath:` 被调用的次数比预期多,我们在滚动时可以感觉到这些额外的计算。因此,我们提供另一个通过索引路径缓存的 API

import DGTemplateLayoutCell

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return tableView.dg_heightForCellWithIdentifier("DGFeedCell", indexPath: indexPath, configuration: { (cell) -> Void in
    // Configure this cell with data, same as what you've done in "-tableView:cellForRowAtIndexPath:"
    // Like:
    //    let cell = cell as! DGFeedCell
    //    cell.loadData(self.feedList[indexPath.section][indexPath.row])
    })
}

或者,如果您的实体有一个唯一的标识符,请使用按键缓存的 API

import DGTemplateLayoutCell

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return tableView.dg_heightForCellWithIdentifier("DGFeedCell", key: uuid, configuration: { (cell) -> Void in
    // Configure this cell with data, same as what you've done in "-tableView:cellForRowAtIndexPath:"
    // Like:
    //    let cell = cell as! DGFeedCell
    //    cell.loadData(self.feedList[indexPath.section][indexPath.row])
    })
}

框架布局模式

FDTemplateLayoutCell 提供了两种询问单元格高度的机制。

  1. 使用 `-systemLayoutSizeFittingSize:` 的自动布局模式
  2. 使用 `-sizeThatFits:` 的框架布局模式

通常不需要关心模式,它将根据单元格内容视图是否已设置自动布局约束自动选择合适的模式。如果您想强制框架布局模式,请在该单元格的配置块中启用此属性

cell.dg_enforceFrameLayout = true

如果您在使用框架布局模式,您必须在自定义单元格中重写 `-sizeThatFits:` 并返回内容视图的高度(不包括分隔符)

override func sizeThatFits(size: CGSize) -> CGSize {
    // var height: CGFloat = 0
    // height += self.contentLabel.sizeThatFits(size).height
    // height += self.contentLabel.sizeThatFits(size).height
    // height += self.contentImageView.sizeThatFits(size).height
    // height += self.userNameLabel.sizeThatFits(size).height
    // height += self.timeLabel.sizeThatFits(size).height
    return CGSizeMake(size.width, height)
}

调试日志

调试日志有助于调试或检查这个“FDTemplateLayoutCell”扩展正在做什么,默认为“false”,在“计算”、“预缓存”或“命中缓存”时打印日志。

self.tableView.dg_debugLogEnabled = true

它会像这样打印

calculate using auto layout - 388.666666666667
cached by indexPath:[0,4] --> 388.666666666667
layout cell created - DGFeedCell
calculate using auto layout - 338.666666666667
cached by indexPath:[0,5] --> 338.666666666667
layout cell created - DGFeedCell
calculate using auto layout - 371.666666666667
cached by indexPath:[0,6] --> 371.666666666667
layout cell created - DGFeedCell
calculate using auto layout - 242.333333333333
cached by indexPath:[0,7] --> 242.333333333333
hit cache by indexPath:[0,0] -> 125.333333333333
hit cache by indexPath:[0,0] -> 125.333333333333
hit cache by indexPath:[0,1] -> 147.0
hit cache by indexPath:[0,1] -> 147.0
hit cache by indexPath:[0,2] -> 352.0
hit cache by indexPath:[0,2] -> 352.0

关于自满足单元格

一个完全自满的单元格受自动布局限制,每个边(“顶部”、“左部”、“底部”、“右部”)至少受到一个布局约束。它与iOS8中引入的“自适应单元格”的概念相同。

不合格的一个 :( - 缺少右侧和底部 非自满

合格的一个人 :)
self-satisfied

备注

模板布局单元格是通过-dequeueReusableCellWithIdentifier:方法创建的,这意味着你必须通过以下方法之一注册此单元格重用标识符:

  • Storyboard中的UITableView的原型单元格。
  • 使用-registerNib:forCellReuseIdentifier:
  • 使用-registerClass:forCellReuseIdentifier:

如果你在天朝

原作者(sunnyxx)文章:http://blog.sunnyxx.com/2015/05/17/cell-height-calculation/

安装

最新版本:1.1.1

pod search DGTemplateLayoutCell 
pod 'DGTemplateLayoutCell', '~> 1.1.1'

如果你找不到最新版本,请尝试

pod setup

发行说明

我们建议在cocoapods中使用最新版本。

  • 1.0

    1. 修改FDTemplateLayoutCell
    2. Swift 2.0编写
  • 1.1

    1. 修复了一些错误。
  • 1.1.1

    1. 添加了一些注释并修改了podspec
  • 1.1.2

    1. 支持Swift 3.0

许可证

MIT