TestsTested | ✗ |
LangLanguage | Obj-CObjective C |
License | MIT |
ReleasedLast Release | Dec 2015 |
Maintained by Luciano Bastos Nunes.
LBNTableViewHandle is available through CocoaPods](http://cocoapods.org). To install simply add the following line to your Podfile:
pod "LBNTableViewHandle"
Now you can pass an NSDictionary as item. And that dictionary may have multiple sections to it.
The default format for dictionary is:
@{@"sections":
@[
@{@"headerConfig":@{},
@"items":@[id, id]
}
]
};
The old way to use this class passing a NSArray is still valid.
Include the file LBNTableViewHandle.h in your class:
#import LBNTableViewHandle.h
After including LBNTableViewHandle.h in the class you will use it, you need to create a global property to keep reference to LBNTableViewHndle instance that can be made this way:
@property (nonatomic, strong) LBNTableViewHandle *mainTableViewHandle;
And this is how you can use it:
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;
OBS: Everything in between "<>" are instructions and have to be replaced by your code.
#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
You can use this class to simplefy your code in case you need to use a tableview inside another tableview. Simply crete an instances of LBNTableViewHandle and attibute then to the respective table datasources and delegates.
Luciano Bastos Nunes, [email protected]
LBNTableViewHandle is available under the MIT license. See the LICENSE file for more info.