本项目目标是通过提供轻量级、可重用和可组合的arylViewDataSources/Delegates组件,定义一个完整的UITableView行为。
显示单个单元格
UITableViewCell *prototype = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"prototype"];
prototype.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
SPLTableViewBehavior *behavior = [[SPLTableViewBehavior alloc] initWithPrototype:prototype configurator:^(UITableViewCell *cell) {
// setup the table view cell
} handler:^{
// do something when tapped
}];
显示数据数组
UITableViewCell *prototype = ...
NSArray *data = @[ @"Object 1", @"Object 2", @"Object 3" ];
SPLArrayBehavior *behavior = [[SPLArrayBehavior alloc] initWithPrototype:prototype data:data configurator:^(UITableViewCell *cell, NSString *object) {
cell.textLabel.text = @"Section 0";
cell.detailTextLabel.text = object;
} handler:^(NSString *object) {
NSLog(@"Do something with %@ when tapped", object);
}];
显示由 NSFetchedResultsController 支持的数据数组
UITableViewCell *prototype = ...
NSFetchedResultsController *controller = ...
SPLFetchedResultsBehavior *behavior = [[SPLFetchedResultsBehavior alloc] initWithPrototype:dataPrototype controller:controller configurator:^(UITableViewCell *cell, ManagedObject *object) {
cell.textLabel.text = @"From CoreData";
cell.detailTextLabel.text = object.name;
}];
将多个行为组合到一个部分中
NSArray *behaviors = @[ behavior1, behavior2, behavior3 ];
SPLSectionBehavior *behavior = [[SPLSectionBehavior alloc] initWithTitle:@"Section 0" behaviors:behaviors];
具有其自己部分的多个行为
NSArray *behaviors = @[ behavior1, behavior2, behavior3 ];
self.behavior = [[SPLCompoundBehavior alloc] initWithBehaviors:behaviors];
将您的最终行为绑定到您的UITableView上
- (void)viewDidLoad
{
[super viewDidLoad];
self.behavior.update = self.tableView.update;
self.tableView.dataSource = self.behavior;
self.tableView.delegate = self.behavior;
}
Oliver Letterer,[email protected]
SPLTableViewBehavior 可在 MIT 许可证下使用。有关更多信息,请参阅 LICENSE 文件。