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文件。