美文网首页ios 开发
iOS脚手架式搭建UITableView

iOS脚手架式搭建UITableView

作者: 小乙的乙 | 来源:发表于2019-05-06 12:20 被阅读0次

    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地址

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

    相关文章

      网友评论

        本文标题:iOS脚手架式搭建UITableView

        本文链接:https://www.haomeiwen.com/subject/joaloqtx.html