一、介绍

在iOS开发中,tableView非常常用,能将其展示出来,它的数据源必不可少。当然数据源有动态下发的,有固定写死的,这里我只探讨固定写死的情况。对于死数据,我们在项目中经常遇到的场景就是我的模块,以及设置模块等。那么,这些死数据我们如何组装的呢?  在以前开发中,我直接用一个可变数组装着每一个cell对应的字典(字典中包含每一个cell需要字段的键值对),虽然也可以实现效果,但是扩展不方便,本人不推荐。 在开发中,我的搭档推荐我项目中的封装的cell模型,我一看,确实不错,在这里给大家分享一下。

二、思想

1、定义继承NSObject的CustomCellConfig类,定义cell可能需要的全部属性

2、定义一个实例化的方法,参数为字典

3、在实例化方法中接着通过OC的setValuesForKeysWithDictionary方法将对应的所有赋值的属性初始化

4、当然再重写一下该类的init方法,默认赋值cell的高为44.0f像素

5、在控制器中创建CustomCellConfig的实体对象并存入数据源,最后刷新tableView

6、获取CustomCellConfig实体对象对cell进行赋值并触发block即可

三、代码

//
// CustomCellConfig.h
// CustomCellConfig
//
// Created by 夏远全 on 2017/12/8.
// Copyright © 2017年 夏远全. All rights reserved.
// #import <Foundation/Foundation.h>
#import <UIKit/UIKit.h> @class CustomCellConfig; typedef void (^CustomCellSelectedBlock)(CustomCellConfig *config); @interface CustomCellConfig : NSObject @property (nonatomic, strong) NSString *title;
@property (nonatomic, strong) NSString *detail;
@property (nonatomic, strong) UIImage *image;
@property (nonatomic, strong) NSString *avatar; @property (nonatomic, assign) int badge;
@property (nonatomic, assign) int contentType;
@property (nonatomic, assign) float height;
@property (nonatomic, assign) BOOL canEdit; @property (nonatomic, assign) UITableViewCellAccessoryType accessoryType;
@property (nonatomic, copy) CustomCellSelectedBlock block;
@property (nonatomic, strong) NSString *viewController;
@property (nonatomic, strong) NSString *segue;
@property (nonatomic, strong) NSIndexPath *indexPath;
@property (nonatomic, copy) NSAttributedString *attributedDetail; @property (nonatomic, strong) NSString *identify;
@property (nonatomic, strong) id object; + (CustomCellConfig *)configWithDictionary:(NSDictionary *)dict; @end
//
// CustomCellConfig.m
// CustomCellConfig
//
// Created by 夏远全 on 2017/12/8.
// Copyright © 2017年 夏远全. All rights reserved.
// #import "CustomCellConfig.h" @implementation CustomCellConfig - (id)init {
if (self = [super init]) {
self.height = 44.0f;
}
return self;
} + (CustomCellConfig *)configWithDictionary:(NSDictionary *)dict {
CustomCellConfig *config = [[CustomCellConfig alloc] init];
[config setValuesForKeysWithDictionary:dict];
return config;
} @end

四、使用

//
// ViewController.m
// CustomCellConfig
//
// Created by 夏远全 on 2017/12/8.
// Copyright © 2017年 夏远全. All rights reserved.
// #import "ViewController.h"
#import "CustomCellConfig.h" @interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (strong,nonatomic)UITableView *tableView;
@property (strong,nonatomic)NSMutableArray *dataSource;
@end @implementation ViewController #pragma mark - life cycle - (void)viewDidLoad {
[super viewDidLoad]; [self setupNavigation];
[self setupDefaultValue];
[self setupSubviews]; [self loadData];
} -(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
} -(void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
} -(void)setupNavigation
{
self.title = @"测试cellConfig";
} -(void)setupDefaultValue
{
self.tableView.backgroundColor = [UIColor whiteColor];
self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
} -(void)setupSubviews
{
[self addSubViews];
[self setupSubviewsConstraints];
} #pragma mark - add subviews -(void)addSubViews
{
[self.view addSubview:self.tableView];
} #pragma mark - layout subviews -(void)setupSubviewsConstraints
{ } #pragma mark - load data
-(void)loadData
{ __weak typeof(self) weakSelf = self; //第一组cell
CustomCellConfig *cellConfig1 = [CustomCellConfig configWithDictionary:@{@"image":[UIImage imageNamed:@"avatar"],@"title":@"消息",@"accessoryType":@(UITableViewCellAccessoryDisclosureIndicator),@"block":^(CustomCellConfig *config){ [weakSelf pushViewController:config]; }}]; CustomCellConfig *cellConfig2 = [CustomCellConfig configWithDictionary:@{@"image":[UIImage imageNamed:@"avatar"],@"title":@"成绩",@"detail":@"分析",@"accessoryType":@(UITableViewCellAccessoryDisclosureIndicator),@"block":^(CustomCellConfig *config){ [weakSelf pushViewController:config]; }}];
CustomCellConfig *cellConfig3 = [CustomCellConfig configWithDictionary:@{@"image":[UIImage imageNamed:@"avatar"],@"title":@"管理",@"attributedDetail":@"",@"accessoryType":@(UITableViewCellAccessoryDisclosureIndicator),@"block":^(CustomCellConfig *config){ [weakSelf pushViewController:config]; }}];
NSArray<CustomCellConfig *> *section1 = @[cellConfig1,cellConfig2,cellConfig3]; //第二组cell
CustomCellConfig *cellConfig4 = [CustomCellConfig configWithDictionary:@{@"image":[UIImage imageNamed:@"avatar"],@"title":@"消息",@"height":@(),@"accessoryType":@(UITableViewCellAccessoryNone),@"block":^(CustomCellConfig *config){ [weakSelf pushViewController:config]; }}];
CustomCellConfig *cellConfig5 = [CustomCellConfig configWithDictionary:@{@"image":[UIImage imageNamed:@"avatar"],@"title":@"成绩",@"height":@(),@"detail":@"分析",@"accessoryType":@(UITableViewCellAccessoryDetailDisclosureButton),@"block":^(CustomCellConfig *config){ [weakSelf pushViewController:config]; }}];
CustomCellConfig *cellConfig6 = [CustomCellConfig configWithDictionary:@{@"image":[UIImage imageNamed:@"avatar"],@"title":@"管理",@"height":@(),@"accessoryType":@(UITableViewCellAccessoryCheckmark),@"block":^(CustomCellConfig *config){ [weakSelf pushViewController:config]; }}];
CustomCellConfig *cellConfig7 = [CustomCellConfig configWithDictionary:@{@"image":[UIImage imageNamed:@"avatar"],@"title":@"更多",@"height":@(),@"accessoryType":@(UITableViewCellAccessoryDetailButton),@"block":^(CustomCellConfig *config){ [weakSelf pushViewController:config]; }}];
NSArray<CustomCellConfig *> *section2 = @[cellConfig4,cellConfig5,cellConfig6,cellConfig7]; //......等等......该模型的字段均可用,自己根据需要进行赋值...........// [self.dataSource addObject:section1];
[self.dataSource addObject:section2];
[self.tableView reloadData];
} #pragma mark - delegate - datasource methods
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return self.dataSource.count;
} -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.dataSource[section] count];
} -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *reuserIdentifier = @"reuserIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuserIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuserIdentifier];
}
CustomCellConfig *cellConfig = self.dataSource[indexPath.section][indexPath.row];
cell.imageView.image = cellConfig.image;
cell.textLabel.text = cellConfig.title;
cell.detailTextLabel.text = cellConfig.detail;
cell.accessoryType = cellConfig.accessoryType;
return cell;
} -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
CustomCellConfig *cellConfig = self.dataSource[indexPath.section][indexPath.row];
return cellConfig.height;
} -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ [tableView deselectRowAtIndexPath:indexPath animated:YES]; CustomCellConfig *cellConfig = self.dataSource[indexPath.section][indexPath.row];
if (cellConfig.block) {
cellConfig.block(cellConfig);
}
} #pragma mark - event rsponse
-(void)pushViewController:(CustomCellConfig *)cellConfig{ UIViewController *vc = [[UIViewController alloc] init];
vc.view.backgroundColor = [UIColor redColor];
[self.navigationController pushViewController:vc animated:YES];
} #pragma mark - public methods #pragma mark - private methods #pragma mark - getters and setters
-(UITableView *)tableView
{
if (!_tableView) {
_tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
_tableView.dataSource = self;
_tableView.delegate = self;
}
return _tableView;
}
-(NSMutableArray *)dataSource{
if (!_dataSource) {
_dataSource = [NSMutableArray array];
}
return _dataSource;
} @end

五、效果

iOS:针对固定数据源,更好的封装cell的更多相关文章

  1. iOS开发之微信聊天工具栏的封装

    之前山寨了一个新浪微博(iOS开发之山寨版新浪微博小结),这几天就山寨个微信吧.之前已经把微信的视图结构简单的拖了一下(IOS开发之微信山寨版),今天就开始给微信加上具体的实现功能,那么就先从微信的聊 ...

  2. wzplayer for ios 针对(mms)优化版本V1.0

    wzplayer for ios针对mms优化版本发布. 1.支持mms,http,rtmp,rtsp等协议 2.支持全格式 下载地址:http://www.coolradio.cn/WzPlayer ...

  3. web自动化针对PO模式进行二次封装之basepage

    在PO模式当中,我们做到了页面对象与测试用例的分离,但在页面对象编写时,我们仍然还有优化的空间.页面对象有一些共同的基本操作,可以封装起来,并可以在基本操作当中加上日志和异常截图的处理.比如说我们在查 ...

  4. iOS开发UI篇—核心动画(UIView封装动画)

    iOS开发UI篇—核心动画(UIView封装动画) 一.UIView动画(首尾) 1.简单说明 UIKit直接将动画集成到UIView类中,当内部的一些属性发生改变时,UIView将为这些改变提供动画 ...

  5. APP自动化针对PO模式进行二次封装之basepage

    APP自动化跟WEB自动化所使用的框架基本一样,都是采用的PO模式结合pytest框架编写自动化测试脚本,为了提高代码的复用性.稳定性和易维护性,我们针对PO模式进行了二次封装,将日志,等待以及异常截 ...

  6. iOS开发之自定义表情键盘(组件封装与自动布局)

    下面的东西是编写自定义的表情键盘,话不多说,开门见山吧!下面主要用到的知识有MVC, iOS开发中的自动布局,自定义组件的封装与使用,Block回调,CoreData的使用.有的小伙伴可能会问写一个自 ...

  7. iOS数据库离线缓存思路和网络层封装

    一直想总结一下关于iOS的离线数据缓存的方面的问题,然后近期也简单的对AFN进行了再次封装.全部想把这两个结合起来写一下.数据展示型的页面做离线缓存能够有更好的用户体验,用户在离线环境下仍然能够获取一 ...

  8. 让iOS开发变得更有效率-分类、工具类

    在工作中整理的一些分类与工具类,分享给大家.这些工具类可以减少项目中的代码量,让代码变得更简洁,可以大大的提升项目的效率,直接拖到项目中使用即可.下载地址:https://github.com/lee ...

  9. iOS菜鸟之FMDB的二次封装简单易用

    闲来无事写点东西,希望大家多多指正! 大家先去git下载FMDB,然后将其中source文件夹中的fmdb文件夹拖入自己的项目中.最后就可以引用下面的代码对fmdb进行一次简单的封装. 这样可以更直观 ...

随机推荐

  1. ThinkPHP中如何获取指定日期后工作日的具体日期

    思路: 1.获取到查询年份内所有工作日数据数组2.获取到查询开始日期在工作日的索引3.计算需查询日期索引4.获得查询日期 /*创建日期类型记录表格*/ CREATE TABLE `tb_workday ...

  2. POJ 3421 X-factor Chains (因式分解+排列组合)

    题意:一条整数链,要求相邻两数前一个整除后一个.给出链尾的数,求链的最大长度以及满足最大长度的不同链的数量. 类型:因式分解+排列组合 算法:因式分解的素因子个数即为链长,链中后一个数等于前一个数乘以 ...

  3. #10 //I [HNOI/AHOI2018]毒瘤

    题解: 80分做法还是听简单的 对于非树边枚举一下端点状态 然而我也不知道为什么就多t了一个点 具体实现上 最暴力的是3^n次 但是我们可以发现对于i不取,j取 i不取,j不取是可以等效成i不取,j没 ...

  4. [ZJOI2006]书架

    链接:https://www.luogu.org/problemnew/show/P2596 题解: 写了两天的平衡树终于大概弄好了所有模板(模板不熟写错debug真是要死) 对于放在头尾,只需要删除 ...

  5. BZOJ1202 [HNOI2005]狡猾的商人 spfa

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1202 题意概括 有一个数列,共n个数字. 告诉你m个区间和,问是否矛盾. 数据组数<=100 ...

  6. 【noip模拟赛5】水流

    描述 全球气候变暖,小镇A面临水灾.于是你必须买一些泵把水抽走.泵的抽水能力可以认为是无穷大,但你必须把泵放在合适的位置,从而能使所有的水能流到泵里.小镇可以认为是N * M的矩阵.矩阵里的每个单元格 ...

  7. 006 Spark中的wordcount以及TopK的程序编写

    1.启动 启动HDFS 启动spark的local模式./spark-shell 2.知识点 textFile: def textFile( path: String, minPartitions: ...

  8. 072 HBase的架构以及各个模块的功能

    一:整体架构 1.体系结构 2.物理模型 3.存储体系 regionserver—>region->多个store(列簇)->一个memstore和多个storefile 4.HDF ...

  9. java enum的一种写法记录

    public enum TestEnum { provider { @Override public void provide() { this.name = "hjzgg"; } ...

  10. Spring根据包名获取包路径下的所有类

    参考mybatis MapperScannerConfigurer.java 最终找到 Spring的一个类  ClassPathBeanDefinitionScanner.java 参考ClassP ...