TFTableDescriptor 2.1.0

TFTableDescriptor 2.1.0

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

Ales Kocur维护。



  • Ales Kocur

文档

快速使用

TFTableDescriptor允许您描述表格内容应该如何组装。

TFTableDescriptor *table = [TFTableDescriptor descriptorWithTable:self.tableView];
TFSectionDescriptor *section;
TFRowDescriptor *row;

section = [TFSectionDescriptor descriptorWithTag:TableSectionTagStaticRows title:@"Section with static rows"];

section.sectionClass = [MyHeaderView class];
row = [TFRowDescriptor descriptorWithRowClass:[MyCustomCell class] data:@"Static row with tag" tag:kRowTagStaticTest];
[section addRow:row];

[table addSection:section];

section = [TFSectionDescriptor descriptorWithTag:TableSectionTagDynamicRows title:@"Section with dynamic rows"];
section.sectionClass = [MyHeaderView class];

row = [TFRowDescriptor descriptorWithRowClass:[MyDynamicCustomCell class] data:@"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis consectetur bibendum gravida. Aliquam vel augue non massa euismod pharetra. Vivamus euismod ullamcorper velit."];
[section addRow:row];

[table addSection:section];

self.tableDescriptor = table

结果可能看起来像这样

单元格

首先,您需要创建单元格。TFTableDescriptor只提供基本的单元格,并且必须创建子类。单元格通过TFBasicDescriptedCell和表头通过TFBasicDescriptedHeaderFooterView。如果您想使用xib创建它们但不知道如何操作,请查看示例。

#import "TFBasicDescriptedCell.h"

@interface MyCustomCell : TFBasicDescriptedCell

@property (strong, nonatomic) IBOutlet UILabel *titleLabel;

@end

您还需要通过提供宏将它们注册到UITableView

REGISTER_CELL_FOR_TABLE(MyButtonCell, self.tableView);
REGISTER_HEADER_FOOTER_FOR_TABLE(MyHeaderView, self.tableView);

如果您希望使用数据配置它们,您必须实现TFTableDescriptorConfigurableCellProtocol的协议方法

- (void)configureWithData:(id)data {

    // If we have suitable data for this cell
    if ([data isKindOfClass:[NSString class]]) {
        // configure cell
        self.titleLabel.text = data;
    }

}

如果您的单元格高度是静态的,您还应该实现+ (NSNumber *)height方法

+ (NSNumber *)height {
    return @44.0;
}

否则,高度将通过自动布局计算 -> 它支持单元格的动态高度。您需要做的是设置您的自动布局。这意味着就应该是从上往下和从左往右的约束。

如果您需要支持iOS 7和不同屏幕大小,您应该在有超过1行的地方使用TFExplicitLabel (UILabel子类)。这对于不同显示尺寸下的正确自动布局计算非常重要。

选择单元格

您可以通过TFTableDescriptor的委托方法访问所选单元格的描述符。如果您想访问其单元格,使用tableDescriptor的cellForRow:方法。

- (void)tableDescriptor:(TFTableDescriptor *)descriptor didSelectRow:(TFRowDescriptor *)rowDescriptor {
    UITableViewCell *cell = [self.tableDescriptor cellForRow:rowDescriptor];
}

插入和删除单元格

您也可以根据单元格的标签或行描述符从表中进行插入或删除操作。

[self.tableDescriptor insertRow:[TFRowDescriptor descriptorWithRowClass:[MyCustomCell class] data:@"IN FRONT OF CELL"] inFrontOfRow:inFrontOfRow rowAnimation:UITableViewRowAnimationLeft];

[self.tableDescriptor removeRow:row rowAnimation:UITableViewRowAnimationRight];

动画类型可以通过rowAnimation参数进行更改。

要运行示例项目,首先复制仓库,然后从Example目录运行pod install

待办事项

  • 使用行描述符定制单元格外观
  • 单元格块和选择器
  • 测试
  • 页脚支持

要求

iOS 7及以上

安装

TFTableDescriptor可通过CocoaPods获取。要安装它,只需将以下行添加到您的Podfile文件中

pod "TFTableDescriptor"

作者

Funtasty
www.thefuntasty.com
Ales Kocur,[email protected]

许可

TFTableDescriptor采用MIT许可证。有关更多信息,请参阅LICENSE文件。