iOS脚手架式搭建UITableView

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]);
                }];
            }];
        }];
    }
联系人效果图

Demo地址

参考文档:找不到地址了,有人知道请留言,不胜感激。

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,390评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,821评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,632评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,170评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,033评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,098评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,511评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,204评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,479评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,572评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,341评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,213评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,576评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,893评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,171评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,486评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,676评论 2 335

推荐阅读更多精彩内容

  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 11,067评论 1 32
  • 一、简介 <<UITableView(或简单地说,表视图)的一个实例是用于显示和编辑分层列出的信息的一种手段 <<...
    无邪8阅读 10,560评论 3 3
  • 新年刚过,美国新闻与世界报道(U.S. News & World Report)就集结了一组健康专家,包括营养学家...
    PurpleZheng阅读 1,148评论 1 2
  • 五月学习总结 (单位:小时) 中文:12.3 外刊:65.2 国商:29.7 学习总结:3.3 练字:0.6 游泳...
    四百分阅读 164评论 0 0