DRTableViewManager 1.0.13

DRTableViewManager 1.0.13

测试测试过的
Lang语言 Obj-CObjective C
许可证 MIT
Released最新发布2015年11月

Darrarski 维护。



  • 作者:
  • Dariusz Rybicki

DRTableViewManager (iOS)

面向对象的UITableViewDataSource和UITableViewDelegate协议代理

对UITableViewDataSource和UITableViewDelegate协议的简单封装,允许通过块或自定义对象轻松配置UITableView内容,这些对象代表部分和行。

DRTableViewManager背后的通用逻辑

  • DRTableViewManager - 实现UITableViewDataSource和UITableViewDelegate协议,拥有DRTableViewSectionController
  • DRTableViewSectionController - 为给定表格视图定义部分(DRTableViewSection)
  • DRTableViewSection - 表示表格视图部分;为该部分定义行(DRTableViewRow);实现与部分相关联的UITableViewDataSource和UITableViewDelegate协议的一部分
  • DRTableViewRow - 表示表格视图行;为该行定义单元格(UITableViewCell);实现与行相关联的UITableViewDataSource和UITableViewDelegate协议的一部分

DRTableViewManager graph

完全支持UITableViewDataSource和UITableViewDelegate协议。例如,DRTableViewSection还定义了部分的标题和页脚(以及它们的高度),而DRTableViewRow定义了UITableViewCell高度,didSelect操作等。

此外,DRTableViewManager被设计成允许在iOS 7上使用UITableViewAutomaticDimension单元格高度。可以通过设置DRTableViewManager对象的automaticRowHeightResolvingType属性来配置解决自动单元格高度的行为。DRTableViewAutomaticRowHeightResolvingType有三种可用选项

  • DRTableViewResolveAutomaticRowHeightAutomatically - 行高将由iOS自动通过UITableViewAutomaticDimension计算(仅在iOS 8上可用)。
  • DRTableViewResolveAutomaticRowHeightManually - 行高将通过解决单元格上的AutoLayout来计算。如果为行设置了tableViewCellForComputingRowHeightAtIndexPathBlock,则DRTableViewManager会自动处理。
  • DRTableViewResolveAutomaticRowHeightAutomaticallyIfAvailable(默认值) - 如果可用,将使用UITableViewAutomaticDimension,否则将像使用DRTableViewResolveAutomaticRowHeightManually选项那样解决高度。

如果您正在使用需要将preferredMaxLayoutWidth设置为正确布局的UIKit元素,请使用DRTableViewResolveAutomaticRowHeightManually选项以避免在不同iOS版本上出现问题。请参阅示例2以获取更多详细信息。

安装

您可以使用Cocoapods将DRTableViewManager集成到您的项目中。为此,您需要在您的Podfile中添加以下某一行:

对于稳定版本(推荐)

pod 'DRTableViewManager', '~> 1.0.13'

这将创建对版本>= 1.0.13< 1.1的依赖关系

您还可以从发行页面下载给定版本的zip压缩档。

使用方法

查看包含的示例项目,或在仓库中的TableViewDemo

示例 1

显示基本用法,其中部分和单元格在数组中以静态方式定义。

示例 2

同样显示了基本用法,但将自动单元格高度配置为基于其内容。适用于iOS 8(使用原生UITableViewAutomaticDimension)和iOS 7(通过AutoLayout计算高度 - 详细信息请参阅源代码)。

TL;DR

示例用法

DRTableViewGenericSectionsController *sectionsController = [[DRTableViewGenericSectionsController alloc] init];
sectionsController.sectionsCountBlock = ^NSInteger {
    return 3;
};
sectionsController.sectionAtIndexBlock = ^NSObject <DRTableViewSection> *(NSInteger sectionIndex) {
    return [DRTableViewGenericSection createWithBlock:^(DRTableViewGenericSection *section) {
        section.tableViewTitleForHeaderInSectionBlock = ^NSString *(UITableView *tableView, NSInteger tableSectionIndex) {
            return [NSString stringWithFormat:@"Section %ld", (long)tableSectionIndex];
        };
        section.tableViewHeightForHeaderInSectionBlock = ^CGFloat(UITableView *tableView, NSInteger tableSectionIndex) {
            return 30;
        };
        section.tableViewNumberOfRowsInSectionBlock = ^NSInteger(UITableView *tableView, NSInteger tableSectionIndex) {
            return 3;
        };
        section.rowAtIndexBlock = ^NSObject <DRTableViewRow> *(NSInteger rowIndex) {
            return [DRTableViewGenericRow createWithBlock:^(DRTableViewGenericRow *row) {
                row.tableViewHeightForRowAtIndexPathBlock = ^CGFloat(UITableView *tableView, NSIndexPath *indexPath) {
                    return 44;
                };
                row.tableViewCellForRowAtIndexPathBlock = ^UITableViewCell *(UITableView *tableView, NSIndexPath *indexPath) {
                    UITableViewCell *cell = [[UITableViewCell alloc] init]; 
                    cell.textLabel.text = [NSString stringWithFormat:@"Row %ld", (long)indexPath.row];
                    return cell;
                };
            }];
        };
    }];
};
_tableViewManager = [[DRTableViewManager alloc] initWithSectionsController:sectionsController];
[_tableViewManager registerInTableView:self.tableView];

变更日志

v1.0.13

修复了iOS 9上手动计算行高的问题

v1.0.12

DRTableViewManager添加了scrollViewDelegate (<UIScrollViewDelegate>)属性

v1.0.11

DRTableViewManager添加了辅助方法

  • rowForCell:atIndexPath
  • sectionForFooterHeaderView:atIndex
v1.0.10

修复了头文件中的导入语句

v1.0.9

DRTableViewManager已更新,添加了sectionAtIndex:rowAtIndexPath:方法

v1.0.8

DRTableViewGenericRowDRTableViewGenericSection的初始化已调整到常见的模式,构造方法名称从createWithBlock:更改为newWithBlock:

v1.0.7

DRTableViewManager添加了单元格缓存,对于计算行高时使用的单元格很有用

示例更新

v1.0.6

DRTableViewManager添加了automaticRowHeightResolvingType属性

示例更新

v1.0.5

DRTableViewRow协议添加了tableView:cellForComputingRowHeightAtIndexPath:方法,以支持iOS 7下的UITableViewAutomaticDimension单元格高度

DRTableViewRow协议添加了tableView:configureCell:forRowAtIndexPath:方法

示例更新

v1.0.4

部署目标已更改为iOS 7.0,DRTableViewManager与iOS 7和iOS 8都兼容。

添加了示例2,展示了如何实现自动单元格高度,该高度在iOS 8上原生支持,但在iOS 7上需要一些额外代码。

v1.0.3

修复了 - 在从UITableViewDelegate协议中调用tableView:heightForRowAtIndexPath:方法时对DRTableViewRow对象的调用错误

v1.0.2

修复了数组中不正确的协议兼容性定义

v1.0.1

一些小修复

v1.0.0

初始发布,支持iOS 8

授权

MIT许可证(MIT)- 查看包含的LICENSE文件