ios界面设计之自定义Cell

采用MVC设计模式

1.Model层设计

新建CJActive文件
CJActive.h文件中

@interface CJActive : NSObject
@property (nonatomic, copy) NSString *icon;
@property (nonatomic, copy) NSString *name;

-(instancetype)initWithDic:(NSDictionary *)dic;
+(instancetype)acitveWithDic:(NSDictionary *)dic;

+(NSMutableArray *)activeList;
@end

CJActive.m文件

-(instancetype)initWithDic:(NSDictionary *)dic{
    if (self = [super init]) {
        [self setValuesForKeysWithDictionary:dic];
    }
    return self;
}
+(instancetype)acitveWithDic:(NSDictionary *)dic{
    return [[self alloc]initWithDic:dic];
}
+(NSMutableArray *)activeList{
    NSArray *dicArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"apps.plist" ofType:nil]];
    //字典转模型
    NSMutableArray *arrayM = [NSMutableArray array];
    
    for (NSDictionary *dic in dicArray) {
        CJActive *active = [CJActive acitveWithDic:dic];
        [arrayM addObject:active];
    }
    return arrayM;

}

2.View层设计

新建CJActiveCell类文件,带xib的。
CJActiveCell.h文件中

@class CJActive;
@interface CJActiveCell : UITableViewCell
@property (nonatomic, strong) CJActive *getData;//定义一个接口,获得数据

+(instancetype) cellWithTableView:(UITableView *)tableView;
@end

CJActiveCell.xib文件中进行自定义cell设计
并且子控件关联至CJActiveCell.m中

@interface CJActiveCell()
@property (weak, nonatomic) IBOutlet UIImageView *icon;
@property (weak, nonatomic) IBOutlet UILabel *title;

@end

在CJActiveCell.m中实现两个方法

//创建cell
+(instancetype)cellWithTableView:(UITableView *)tableView{
    static NSString *reuseID= @"activeCell";
    //1.创建可重用的cell
    CJActiveCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseID];
    //2.判断为空,一开始初始化
    if (cell == nil) {
        cell = [[[NSBundle mainBundle]loadNibNamed:@"CJActiveCell" owner:nil options:nil]lastObject];
    }
    //3.返回
    return cell;
    
}
//重写getData属性的setter方法,当getData被赋值的时候,就会调用setter方法,
//在方法里面对cell的内容进行设置,也就是一拿到数据,就设置值
-(void)setGetData:(CJActive *)getData{
    _getData = getData;
    self.title.text =getData.name;
   //.......
    } ];

}

3.Controller层设计

CJActiveController.m文件实现

@interface CJActiveController ()<UITableViewDataSource>
@property (nonatomic, strong) NSArray *activeInfo;//定义一个属性,加载数据
@end

@implementation CJActiveController
 
//懒加载需要的数据
-(NSArray *)activeInfo{
    if (_activeInfo==nil) {        
        _activeInfo = [CJActive activeList];      
    }
    return _activeInfo;   
}

#pragma mark 数据源方法
-(NSInteger)numberOfSectionsInTableView:(nonnull UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.activeInfo.count;
}

-(UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath{
    
    //1.创建可重用的自定义cell
    CJActiveCell *cell =[CJActiveCell cellWithTableView:tableView];
    
    //2.设置值
    CJActive *active = self.activeInfo[indexPath.row];
    cell.getData =active;
  
    //3.return
    return  cell;
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • *面试心声:其实这些题本人都没怎么背,但是在上海 两周半 面了大约10家 收到差不多3个offer,总结起来就是把...
    Dove_iOS阅读 27,401评论 30 472
  • 传统模式下的开发MVCMVVM基于面向协议MVP的介绍MVP实战开发说在前面:相信就算你是个iOS新手也应该听说过...
    行走的菜谱阅读 8,403评论 1 5
  • 基于面向协议MVP模式下的软件设计-(iOS篇) 传统模式下的开发MVCMVVM基于面向协议MVP的介绍MVP实战...
    风之痕_阅读 16,422评论 29 119
  • 张同学的头衔太多,但大多跟社会地位没什么关系。张同学这一年多来,做的最称职的职业并不是本职的质量管理,而是逗比三陪...
    李小竹子阅读 1,526评论 1 1
  • 这是网易云音乐中的一段评论,被它的文字所打动,因为我的男朋友从来不会给我说这些甜言蜜语,在他看来,行动远比语言重要...
    古明阳阅读 2,915评论 0 0

友情链接更多精彩内容