PBITableView is a kind of delightful tableview using MVVM.Compare the old ways, it provides a simply function to create the every single section, which makes creating complex tableview likes building blocks.What's more, it is convenient to provides some cell styles for static view.Hope it could help the iOS developers.
This type of tableview has several feature:
- regular cell style but includes different user interaction component like
switch
,textfield
,label
and so on. - there are many similar views in different apps.
- writing code for this view in a waste of time and energy.
Therefor we need to improve it.
self.tableview.delegate = self;
self.tableview.datasource = self;
//Delagte and DataSource func
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
return cell;
}
Obviously, in this way, we need realize so many these functions and it is annoying especially we just want to realize some simple tableviews.
PBITableView uses this simple functions to create:
-(void)init{
[self initTableView];
[self addBaseSection];
}
-(void)addBaseSection{
[self.tableView headerView:^(PBITableViewSectionModel *sectionModel){
//Set HeaderView
} withCells:^(NSMutableArray *cellArray) {
//Set CellModel Arrays
PBITableViewCellModel *cellModel = [PBITableViewCellModel new];
cellModel.height = 50;
cellModel.renderBlock=^UITableViewCell *(NSIndexPath *indexPath, UITableView *tableView) {
//Init Cell Instance
return cell;
};
};
[cellArray addObject:cellModel];
};
} footerView:^(PBITableViewSectionModel *sectionModel){
//Set footerView
}];
}
Above all, it is the code about how to create a tableview with a section.All u need is realize the implementation of your cells and add the cellArray
.U can set the appearance of section headerView and footerView By setting sectionModel
properties.
I have written a demo in project.U can look up in detail.
PBIManageCell
provides 5 styles cell.Each cells have their own styles too.Pick the style
when init the cell.
For example, I would use the PBIMangeSwitchCell
to express it.
//Init Func pick style through PBIMangeSwitchCellStyle
+(PBIMangeSwitchCell *)cellWithIdentifier:(NSString *)cellIdentifier tableView:(UITableView *)tableView style:(PBIMangeSwitchCellStyle)style;
//Cell Instance Implementation
PBIMangeSwitchCell *cell = [PBIMangeSwitchCell cellWithIdentifier:@"123" tableView:tableView style:PBIMangeSwitchCellStyleWithLeftIcon];
cell.titleLabel.text = @"Switch Change Cell Background";
cell.leftIcon.image = [UIImage imageNamed:@"warning"];
__weak __typeof__(PBIMangeSwitchCell *) weak_cell = cell;
cell.switchBlock = ^(BOOL switchValue) {
if (switchValue == YES) {
weak_cell.backgroundColor = [UIColor greenColor];
}else{
weak_cell.backgroundColor = [UIColor yellowColor];
}
};
According to the different cells, it provides aimed properties and blocks to edit.
Because of the block usage, please notice the retain cycle problem.
u can use this __weak __typeof__(object) weak_object = object
and __strong __typeof__(object) strong_object = object
to avoid this problem
There are two main parts : PBIManageCell
and PBITableView
PBIManageCell
contains:
PBIManageCellConst.h
UIView+PBIManageLayout
PBIManageBaseCell.h
PBIMangeSwitchCell.h
PBIManageCustomCell.h
PBIManageLabelCell.h
PBIManageTextFieldCell.h
PBITableView
contains:
PBITableView.h
PBITableViewModel.h
PBITableViewSectionModel.h
PBITableViewCellModel.h
#import PBIManageCell.h
#import PBITableView.h
//Then use the function in "The new ways to create" :)
PBITableView is released under the MIT license. See LICENSE for details.