iOS腳手架式搭建UITableView

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)系人效果圖

Demo地址

參考文檔:找不到地址了,有人知道請(qǐng)留言,不勝感激。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Swift1> Swift和OC的區(qū)別1.1> Swift沒(méi)有地址/指針的概念1.2> 泛型1.3> 類型嚴(yán)謹(jǐn) 對(duì)...
    cosWriter閱讀 11,621評(píng)論 1 32
  • 一、簡(jiǎn)介 <<UITableView(或簡(jiǎn)單地說(shuō),表視圖)的一個(gè)實(shí)例是用于顯示和編輯分層列出的信息的一種手段 <<...
    無(wú)邪8閱讀 10,958評(píng)論 3 3
  • 新年剛過(guò),美國(guó)新聞與世界報(bào)道(U.S. News & World Report)就集結(jié)了一組健康專家,包括營(yíng)養(yǎng)學(xué)家...
    PurpleZheng閱讀 1,241評(píng)論 1 2
  • 五月學(xué)習(xí)總結(jié) (單位:小時(shí)) 中文:12.3 外刊:65.2 國(guó)商:29.7 學(xué)習(xí)總結(jié):3.3 練字:0.6 游泳...
    四百分閱讀 229評(píng)論 0 0

友情鏈接更多精彩內(nèi)容