Flutter火了,很多人用它,性能很好。多平臺開發。其中腳手架式的設計感覺很爽。盡量兼容H5 andorid ios的開發習慣。這點很不容易。
剛入行的時候看過某個大神寫的Demo對UITableView 封裝一層,用起來有點像腳手架的感覺。可惜Demo找不到了。廢話不說了上代碼。
@weakify(self);
// 在tableView上添加一個section
[self addSecion:^(MDScaffoldTableViewSection *tableViewSection, NSInteger sectionIndex) {
@strongify(self);
tableViewSection.sectionTitle = @"T";
//在這個section上添加一個cell
[tableViewSection addCell:^(MDScaffoldCellConfig *cellConfig, UITableViewCell *cell, NSIndexPath *indexPath) {
cellConfig.cellStyle = UITableViewCellStyleDefault;
cellConfig.cellHeight = 40;
cellConfig.editenble = YES; // 能否編輯只需要配置下即可
cell.textLabel.text = @"通訊錄";
} whenSelectedCell:^(NSIndexPath *indexPath) { // cell 被點擊觸發的事件
MDAddressBookViewController *adVC = [[MDAddressBookViewController alloc]init];
[self.navigationController pushViewController:adVC animated:YES];
}];
[tableViewSection addCell:^(MDScaffoldCellConfig *cellConfig, UITableViewCell *cell, NSIndexPath *indexPath) {
cellConfig.cellStyle = UITableViewCellStyleDefault;
cellConfig.cellHeight = 50;
cellConfig.editenble = NO;// 能否編輯只需要配置下即可
cell.textLabel.text = @"美女照片";
} whenSelectedCell:^(NSIndexPath *indexPath) {
}];
}];
// 在tableView上再添加一個section
[self addSecion:^(MDScaffoldTableViewSection *tableViewSection, NSInteger sectionIndex) {
tableViewSection.sectionTitle = @"O";// 還沒有實現
[tableViewSection addCell:^(MDScaffoldCellConfig *cellConfig, UITableViewCell *cell, NSIndexPath *indexPath) {
cellConfig.cellStyle = UITableViewCellStyleDefault;
cellConfig.cellHeight = 40;
cellConfig.editenble = YES;
cell.textLabel.text = @"其他";
} whenSelectedCell:^(NSIndexPath *indexPath) {
// cell點擊事件
}];
}];
效果圖
腳手架搭建tableview效果圖
編輯cell效果圖
優點:邏輯能放在一起,不需要每寫一個UITableView都去再寫一次。而且代理的邏輯都要放在代理方法中,邏輯會比較凌亂。
如何實現呢?
- 我們把每一個cell的高度,樣式,點擊事件,能否編輯,能否移動都看成cell的屬性,用一個對象MDScaffoldCellConfig來收集這些數據。
@implementation MDScaffoldCellConfig
- (instancetype)init
{
self = [super init];
if (self) {
self.reuseIdentifer = @"defaultIdentifer";
self.moveble = YES;
self.editenble = NO;
self.cellStyle = UITableViewCellStyleDefault;
self.cellHeight = UITableViewAutomaticDimension;
self.tableViewSuperClass = [UITableViewCell class];// 默認系統自帶樣式
self.editingStyle = UITableViewCellEditingStyleNone;
}
return self;
}
@end
- 封裝一個Section類,用來管理cellConfig對象,控制cell的添加和移除。
/**
添加一個cell配置及事件
@param cellConfigBlock 配置
@param selectCellBlock 點擊事件
*/
- (void)addCell:(MDTableViewCellConfigBlock)cellConfigBlock whenSelectedCell:(MDStaticCellWhenDidSeclectedBlock )selectCellBlock
{
if (!self.cellContainer) {
self.cellContainer = [NSArray array];
}
MDScaffoldCellConfig *config = [[MDScaffoldCellConfig alloc]init];
config.configBlock = [cellConfigBlock copy];
config.whenSelectBlock = [selectCellBlock copy];
cellConfigBlock(config,nil,nil);
self.cellContainer = [self.cellContainer arrayByAddingObject:config];
}
/**
移除index位置上的cell
@param rowIndex rowi index
@param annited 是否有動畫
*/
- (void)removeCellAtIndex:(NSInteger)rowIndex annimated:(BOOL)annited
{
NSMutableArray *cells = [self.cellContainer copy];
[cells removeObjectAtIndex:rowIndex];
if (annited) {
[self.tableView beginUpdates];
// 默認只有一個Section
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:rowIndex inSection:0]] withRowAnimation:UITableViewRowAnimationLeft];
[self.tableView endUpdates];
}else
{
[self.tableView beginUpdates];
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:rowIndex inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
[self.tableView endUpdates];
}
}
- 最后在tableView的代理中實現這些配置即可.
#pragma -mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return self.sectionContainer.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
MDScaffoldTableViewSection *sections = [self.sectionContainer objectAtIndex:section];
return sections.cellContainer.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
MDScaffoldTableViewSection *setcionItem = [self.sectionContainer objectAtIndex:indexPath.section];
MDScaffoldCellConfig *configitem = [setcionItem.cellContainer objectAtIndex:indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:configitem.reuseIdentifer];
if (!cell) {
cell = [[configitem.tableViewSuperClass alloc]initWithStyle:configitem.cellStyle reuseIdentifier:configitem.reuseIdentifer];
}
configitem.configBlock(nil,cell,indexPath);
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
MDScaffoldTableViewSection *setcionItem = [self.sectionContainer objectAtIndex:indexPath.section];
MDScaffoldCellConfig *configitem = [setcionItem.cellContainer objectAtIndex:indexPath.row];
return configitem.cellHeight;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (!tableView.editing && !tableView.allowsMultipleSelection) {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
if (tableView.editing && !tableView.allowsMultipleSelectionDuringEditing) {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
MDScaffoldTableViewSection *section = [self.sectionContainer objectAtIndex:indexPath.section];
MDScaffoldCellConfig *contentCell = [section.cellContainer objectAtIndex:indexPath.row];
if (contentCell.whenSelectBlock) {
contentCell.whenSelectBlock(indexPath);
}
}
在實現一些靜態的Cell 特別方便。動態的cell也能實現。不過特別要注意循環引用的問題。
寫個通訊錄看代碼有多么簡單
for (int i = 0; i < self.sectionTitleArray.count; i++) {
@weakify(self);
[self addSecion:^(MDScaffoldTableViewSection *tableViewSection, NSInteger sectionIndex) {
@strongify(self);
tableViewSection.sectionTitle = self.sectionTitleArray[i]; // title
NSArray *sectionData = [self.dataSource objectForKey:self.sectionTitleArray[I]];
[sectionData enumerateObjectsUsingBlock:^(NSDictionary* _Nonnull dict, NSUInteger idx, BOOL * _Nonnull stop) {
[tableViewSection addCell:^(MDScaffoldCellConfig *cellConfig, UITableViewCell *cell, NSIndexPath *indexPath) {
cellConfig.cellStyle = UITableViewCellStyleValue1;
cellConfig.cellHeight = 50;
cell.textLabel.text = [dict objectForKey:@"name"];
cell.detailTextLabel.text = [(NSArray *)[dict objectForKey:@"phones"] firstObject];
} whenSelectedCell:^(NSIndexPath *indexPath) {
NSLog(@"點擊了 %@ - %@", [dict objectForKey:@"name"],[(NSArray *)[dict objectForKey:@"phones"] firstObject]);
}];
}];
}];
}
聯系人效果圖
參考文檔:找不到地址了,有人知道請留言,不勝感激。