AFDynamicTableHelper 0.1.0

AFDynamicTableHelper 0.1.0

测试已测试
语言语言 Obj-CObjective C
许可证 MIT
发布最后发布2015年3月

appFigures维护。



使用 iOS 7.0 和更高版本创建动态高度的表格单元格。

您正在构建一个以 iOS 7.0 和更高版本为目标的 app,并需要创建具有动态单元格高度的表格视图吗?这个小助手会处理您为此目的需要做的所有复杂事情。在 iOS 8.0 中,它会自动利用新的自动大小功能。

如果您只针对 8.0 和更高版本,则不需要此类。只需使用内置的 UITableViewAutomaticDimension

还在这里?继续阅读

如果您对实现 iOS 7 动态高度表格视图所涉及的问题不熟悉,强烈建议您阅读这个 Stackoverflow 问题。此类的大部分代码基于该问题。

用法

此实用工具是一个简单的 NSObject,为您提供实现复杂功能的实际实施方案

tableView:heightForRowAtIndexPath:

tableView:cellForRowAtIndexPath:

一个简单的用例看起来可能像这样

@implementation MyTableViewController
...
- (void)awakeFromNib {
    self.tableHelper = [[AFDynamicTableHelper alloc] init];
    self.tableHelper.delegate = self;
    self.tableHelper.reusableCellIdentifier = @"myCell";
}

/* ... the usual tableView delegate methods... */

// Just forward these methods over to the dynamic table helper and get on with your day.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [self.tableHelper tableView:tableView cellForRowAtIndexPath:indexPath];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [self.tableHelper tableView:tableView heightForRowAtIndexPath:indexPath];
}

演示项目中查看完整的示例。

自定义您的单元格

为了施展魔法,此类需要管理单元格的创建和填充方式。它唯一的要求是,在常规的 tableView:cellForRowAtIndexPath: 中设置单元格,您需要实现以下代理方法

- (void)dynamicTableHelper:(AFDynamicTableHelper *)tableHelper
               prepareCell:(UITableViewCell *)cell
               atIndexPath:(NSIndexPath *)indexPath
                 tableView:(UITableView *)tableView
                 offscreen:(BOOL)offscreen
{
    // `cell` is created for you based on
    // tableHelper.reusableCellIdentifier. You just
    // need to populate it.

    // `offscreen` specifies if this cell will be used solely for height
    // measurements purposes so you can avoid doing
    // any expensive setup steps that don't affect the
    // cell's height.

    // the `offscreen` argument is only here for optimization purposes
    // and can be safely ignored if you don't want to deal
    // with it.
}

在 iOS > = 8.0 时会发生什么?

当此类检测到 iOS >= 8.0 时,它的工作量大约减少了 95%,并且当询问单元格的高度时,只需返回 UITableViewAutomaticDimension。关键在于您不需要根据 iOS 的版本更改代码。这种逻辑是在幕后处理的。

如果我使用多个单元格原型会发生什么?

只需实现代理方法 dynamicTableHelper:reusableCellIdentifierForRowAtIndexPath:tableView:

安装

作者

Oz Michaeli from appFigures

许可证

AFDynamicTableHelper 在 MIT 许可证下可用。有关更多信息,请参阅 LICENSE 文件。