测试已测试 | ✗ |
Lang语言 | Obj-CObjective C |
许可证 | MIT |
Released最后发布 | 2015年12月 |
由 Luciano Bastos Nunes 维护。
LBNTableViewHandle 通过 CocoaPods 提供](https://cocoapods.org.cn)。要安装,只需将以下行添加到您的 Podfile 中
pod "LBNTableViewHandle"
现在您可以传递一个 NSDictionary 作为项目。该字典可以有多个部分。
字典的默认格式为
@{@"sections":
@[
@{@"headerConfig":@{},
@"items":@[id, id]
}
]
};
使用此类并传递 NSArray 的旧方法仍然有效。
将 LBNTableViewHandle.h 文件包含到您的类中
#import LBNTableViewHandle.h
在将 LBNTableViewHandle.h 包含到您将使用的类中之后,您需要创建一个全局属性以保持对 LBNTableViewHandle 实例的引用,可以按以下方式创建
@property (nonatomic, strong) LBNTableViewHandle *mainTableViewHandle;
这样就可以使用了
self.mainTableViewHandle = [[LBNTableViewHandle alloc] initWithItems:<NSARRAY WITH THE ITENS TO BE SHOWN IN THE TABLE VIEW OR A NSDICTIONARY WITH THE ABOVE FORMAT> CellIdentifier:^NSString *(id item) {
return <RETURN CELL IDENTIFIER, AS IN YOUR TABLEVIEWCELL AT STORYBOARD, FOR AN ITEM>;
} ConfigureCell:^(id cell, id item, NSIndexPath *indexPath) {
<CONFIGURE YOUR CELL>
} DeleteCell:<NOT USED IN THIS VERSION KEEP IT NIL> HeightForItem:^CGFloat(id item) {
return <RETURN CELL HEIGHT FOR AN ITEM>;
} DidSelect:^(NSIndexPath *indexPath, id item) {
<WHAT TO DO WHEN A ITEM IS SELECTED IN THE LIST>
} ViewForSectionHeader:^UIView *(NSInteger section, id item) {
<CREATE AND RETURN THE VIEW THAT WILL BE A HEADER FOR THIS SECTION>
OBS: <<item>> is the content of headerConfig key
} HeightForHeader:^CGFloat(NSInteger section, id item) {
<RETURN THE HEIGHT FOR THIS SECTIONS HEADER>
OBS: <<item>> is the content of headerConfig key
}];
self.mainTableView.dataSource = self.mainTableViewHandle;
self.mainTableView.delegate = self.mainTableViewHandle;
注意:所有 "<>" 之间的内容都是指令,必须用您的代码替换。
#import LBNTableViewHandle.h
@interface MainViewController ()
@property (nonatomic, week) IBOutlet UITableView *mainTableView;
@property (nonatomic, strong) LBNTableViewHandle *mainTableViewHandle;
@end
@implementation MainViewController
- (void)viewDidLoad {
NSDictionary *sections =
@{@"sections":
@[
@{@"headerConfig":@{@"title":@"Novidades"},
@"items":@[@"Title 1", @"Title 2"]
}
]
};
self.mainTableViewHandle = [[LBNTableViewHandle alloc] initWithItems:sections CellIdentifier:^NSString *(id item) {
return @"MyCellIdentifier";
} ConfigureCell:^(id cell, id item, NSIndexPath *indexPath) {
UITableViewCell *cell = cell;
cell.title = item[indexPath.row];
} DeleteCell:nil HeightForItem:^CGFloat(id item) {
return 44;
} DidSelect:^(NSIndexPath *indexPath, id item) {
} ViewForSectionHeader:^UIView *(NSInteger section, id item) {
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.mainTableView.frame.size.width, 24)];
/* Create custom view to display section header... */
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, self.mainTableView.frame.size.width, 18)];
[label setFont:[UIFont boldSystemFontOfSize:12]];
NSString *string = item[@"title"];
[label setText:string];
[view addSubview:label];
[view setBackgroundColor:[UIColor colorWithRed:166/255.0 green:177/255.0 blue:186/255.0 alpha:1.0]]; //your background color...
return view;
} HeightForHeader:^CGFloat(NSInteger section, id item) {
return 30.0f;
}];
self.enableDeselectOnDidSelect = YES;
self.mainTableView.dataSource = self.mainTableViewHandle;
self.mainTableView.delegate = self.mainTableViewHandle;
}
@end
您可以使用此类来简化您的代码,如果您需要在另一个表格视图内部使用表格视图。只需创建 LBNTableViewHandle 的实例并将它们分别分配给相应的表格数据源和代理。
Luciano Bastos Nunes,[email protected]
LBNTableViewHandle 在 MIT 许可证下提供。有关更多信息,请参阅 LICENSE 文件。