简化UITableView,UICollectionView,UITableViewCell的使用。
还包括一个用于YapDatabase的UITableView。
pod "GHUITable"
pod "GHUITable/Yap" # Optional, for YapDatabase support
观看并了解GHUITable,最佳方式是通过其使用实例。克隆仓库并打开示例项目。
GHUITableView *tableView = [[GHUITableView alloc] init];
// These are all the cells that will be used, usually it's just one.
[tableView registerClasses:@[GHUITextImageCell.class]];
// This block returns the cell class.
// It can be based on the data or section, or (in this case) can be constant.
tableView.dataSource.classBlock = ^Class(id object, NSIndexPath *indexPath) {
return GHUITextImageCell.class;
};
// Set the data on the cell.
tableView.dataSource.cellSetBlock = ^(GHUITextImageCell *cell, GHData *data, NSIndexPath *indexPath, UITableView *tableView, BOOL dequeued) {
[cell.viewForContent setName:data.name description:data.description image:[UIImage imageNamed:data.imageName]];
};
// Set what happens when a user selects a cell.
tableView.dataSource.selectBlock = ^(UITableView *tableView, NSIndexPath *indexPath, GHData *data) {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
NSLog(@"Selected: %@", indexPath);
// Do something
};
// Set section headers
[tableView.dataSource setHeaderText:@"Section 1" section:0];
[tableView.dataSource setHeaderText:@"Section 2" section:1];
有众多方法可以用来添加、更新或删除数据。
[tableView setObjects:@[...] animated:NO];
[tableView addObjects:@[...] animated:NO];
// And many more...
或者访问数据源上的方法。(如果不在乎索引路径的变化,可以传递nil。)
[tableView.dataSource addObjects:@[..] section:0 indexPaths:nil];
[tableView.dataSource removeObjects:@[..] section:0 indexPaths:nil];
// And many more...
// If you edit the datasource directly be sure to call reloadData
[tableView reloadData];
这允许你只用几行代码将任何视图转为UITableViewCell。
例如,GHUITextImageCell
定义为
@interface GHUITextImageCell : GHUITableViewCell
@end
@implementation GHUITextImageCell
+ (Class)contentViewClass { return GHUITextImageView.class; }
@end
GHUITextImageView 也是一个使用 YOLayout 的视图示例,你也应该查看!
这是一个具有不同section的cell类的示例。
GHUITableView *tableView = [[GHUITableView alloc] init];
// These are the cells that will be used.
[tableView registerClasses:@[GHUISwitchCell.class, GHUITextImageCell.class]];
// This block chooses the cell class based on the section, but you could also do it based on the data.
tableView.dataSource.classBlock = ^Class(NSDictionary *object, NSIndexPath *indexPath) {
if (indexPath.section == 2) return GHUISwitchCell.class;
return GHUITextImageCell.class;
};
// Set the data on the cell. (Cell depends on section.)
tableView.dataSource.cellSetBlock = ^(GHUITableViewCell *cell, NSDictionary *dict, NSIndexPath *indexPath, UITableView *tableView, BOOL dequeued) {
if (indexPath.section == 0) {
[cell.viewForContent setName:dict[@"name"] description:dict[@"description"] image:[UIImage imageNamed:dict[@"imageName"]]];
} else if (indexPath.section == 1) {
[cell.viewForContent setName:dict[@"name"] description:dict[@"description"] imageURLString:dict[@"imageURLString"]];
} else if (indexPath.section == 2) { // or [cell isKindOfClass:GHUISwitchCell.class]
[cell.viewForContent setTitle:dict[@"title"] description:dict[@"description"] on:[dict[@"on"] boolValue]];
}
};
有时即使你有静态内容也要使用表格视图也是很有用的。您可以将UIView添加到数据源中而不是数据。这样就会绕过cell渲染模式,因此不适合内容非常多的情况。
GHUITableView *tableView = [[GHUITableView alloc] init];
// You can add a view as a data object source.
// In this scenario you are not re-using views and is appropriate for when you have static content.
GHUITextImageView *view = [[GHUITextImageView alloc] init];
[view setName:@"Name2" description:@"This is a description #2" image:[UIImage imageNamed:@"Preview2-Filled"]];
[tableView addObjects:@[view] section:0 animated:NO];
// You can even add a cell as a data object source (although this is probably a little awkward, can be useful sometimes).
GHUITextImageCell *cell = [[GHUITextImageCell alloc] init];
[cell.viewForContent setName:@"Name1" description:@"This is a description #1" image:[UIImage imageNamed:@"Preview2"]];
[tableView addObjects:@[cell] section:1 animated:NO];
GHUICollectionView *view = [[GHUICollectionView alloc] init];
view.backgroundColor = [UIColor grayColor];
// These are all the cells that will be used, usually it's just one.
[view registerClasses:@[GHUITextImageCollectionCell.class]];
// This block returns the cell class.
// It can be based on the data or section, or (in this case) can be constant.
view.dataSource.classBlock = ^Class(id object, NSIndexPath *indexPath) {
return GHUITextImageCollectionCell.class;
};
// Set the data on the cell.
view.dataSource.cellSetBlock = ^(GHUITextImageCollectionCell *cell, NSDictionary *dict, NSIndexPath *indexPath, UITableView *tableView, BOOL dequeued) {
[cell.viewForContent setName:dict[@"name"] description:dict[@"description"] image:[UIImage imageNamed:dict[@"imageName"]]];
};
// Set what happens when a user selects a cell.
view.dataSource.selectBlock = ^(UICollectionView *collectionView, NSIndexPath *indexPath, NSString *object) {
[collectionView deselectItemAtIndexPath:indexPath animated:YES];
NSLog(@"Selected: %@", indexPath);
// Do something
};
// Set header view
[_view registerHeaderClass:GHUICollectionHeaderLabelView.class];
_view.dataSource.headerViewBlock = ^(UICollectionView *collectionView, GHUICollectionHeaderLabelView *view, NSInteger section) {
view.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.8];
view.label.textColor = [UIColor colorWithRed:255.0f/255.0f green:125.0f/255.0f blue:0.0f/255.0f alpha:1.0];
view.label.font = [UIFont systemFontOfSize:18];
view.label.text = [NSString stringWithFormat:@"Section %@", @(section)];
};
// The data
[_view setObjects:
@[
@{@"name": @"Gastropub swag pork belly, butcher selvage mustache chambray scenester pour-over.",
@"description": @"Cosby sweater stumptown Carles letterpress, roof party deep v gastropub next level. Tattooed bitters distillery, scenester PBR&B pork belly swag twee DIY. Mixtape plaid Carles photo booth sustainable you probably haven't heard of them. Vice normcore fap Thundercats Williamsburg Truffaut paleo small batch, plaid PBR&B Brooklyn jean shorts. Next level lomo direct trade farm-to-table, cred hoodie post-ironic fingerstache pop-up put a bird on it. Keytar PBR literally, DIY Bushwick Pinterest bicycle rights.",
@"imageName": @"Preview2"},
@{@"name": @"YOLO irony beard",
@"description": @"Raw denim Tumblr roof party beard gentrify pickled, art party ethical",
@"imageName": @"Preview2"}
] section:0];
示例即将推出。