Flutter火了,很多人用它,性能很好。多平臺(tái)開(kāi)發(fā)。其中腳手架式的設(shè)計(jì)感覺(jué)很爽。盡量兼容H5 andorid ios的開(kāi)發(fā)習(xí)慣。這點(diǎn)很不容易。
剛?cè)胄械臅r(shí)候看過(guò)某個(gè)大神寫的Demo對(duì)UITableView 封裝一層,用起來(lái)有點(diǎn)像腳手架的感覺(jué)??上emo找不到了。廢話不說(shuō)了上代碼。
@weakify(self);
// 在tableView上添加一個(gè)section
[self addSecion:^(MDScaffoldTableViewSection *tableViewSection, NSInteger sectionIndex) {
@strongify(self);
tableViewSection.sectionTitle = @"T";
//在這個(gè)section上添加一個(gè)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 被點(diǎn)擊觸發(fā)的事件
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上再添加一個(gè)section
[self addSecion:^(MDScaffoldTableViewSection *tableViewSection, NSInteger sectionIndex) {
tableViewSection.sectionTitle = @"O";// 還沒(méi)有實(shí)現(xiàn)
[tableViewSection addCell:^(MDScaffoldCellConfig *cellConfig, UITableViewCell *cell, NSIndexPath *indexPath) {
cellConfig.cellStyle = UITableViewCellStyleDefault;
cellConfig.cellHeight = 40;
cellConfig.editenble = YES;
cell.textLabel.text = @"其他";
} whenSelectedCell:^(NSIndexPath *indexPath) {
// cell點(diǎn)擊事件
}];
}];
效果圖

腳手架搭建tableview效果圖

編輯cell效果圖
優(yōu)點(diǎn):邏輯能放在一起,不需要每寫一個(gè)UITableView都去再寫一次。而且代理的邏輯都要放在代理方法中,邏輯會(huì)比較凌亂。
如何實(shí)現(xiàn)呢?
- 我們把每一個(gè)cell的高度,樣式,點(diǎn)擊事件,能否編輯,能否移動(dòng)都看成cell的屬性,用一個(gè)對(duì)象MDScaffoldCellConfig來(lái)收集這些數(shù)據(jù)。
@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];// 默認(rèn)系統(tǒng)自帶樣式
self.editingStyle = UITableViewCellEditingStyleNone;
}
return self;
}
@end
- 封裝一個(gè)Section類,用來(lái)管理cellConfig對(duì)象,控制cell的添加和移除。
/**
添加一個(gè)cell配置及事件
@param cellConfigBlock 配置
@param selectCellBlock 點(diǎn)擊事件
*/
- (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 是否有動(dòng)畫
*/
- (void)removeCellAtIndex:(NSInteger)rowIndex annimated:(BOOL)annited
{
NSMutableArray *cells = [self.cellContainer copy];
[cells removeObjectAtIndex:rowIndex];
if (annited) {
[self.tableView beginUpdates];
// 默認(rèn)只有一個(gè)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的代理中實(shí)現(xiàn)這些配置即可.
#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);
}
}
在實(shí)現(xiàn)一些靜態(tài)的Cell 特別方便。動(dòng)態(tài)的cell也能實(shí)現(xiàn)。不過(guò)特別要注意循環(huán)引用的問(wèn)題。
寫個(gè)通訊錄看代碼有多么簡(jiǎn)單
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(@"點(diǎn)擊了 %@ - %@", [dict objectForKey:@"name"],[(NSArray *)[dict objectForKey:@"phones"] firstObject]);
}];
}];
}];
}

聯(lián)系人效果圖
參考文檔:找不到地址了,有人知道請(qǐng)留言,不勝感激。