博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS模型以及使用
阅读量:6368 次
发布时间:2019-06-23

本文共 4016 字,大约阅读时间需要 13 分钟。

个人习惯,也可以不这样写

创建模型基类:

#import 
@interface WJBaseModel : NSObject//将字典内的值赋给申明的相应属性- (instancetype)initWithDict:(NSDictionary *)dict;+ (instancetype)modelWithDict:(NSDictionary *)dict;@end
#import "WJBaseModel.h"@implementation WJBaseModel- (instancetype)initWithDict:(NSDictionary *)dict {    self = [super init];    if (self) {            }    return self;}+ (instancetype)modelWithDict:(NSDictionary *)dict {    return [[self alloc]initWithDict:dict];}@end

创建模型:

#import 
#import "WJBaseModel.h"@interface AddressModel : WJBaseModel@property (nonatomic,copy)NSString *name;//名称@property (nonatomic,copy)NSString *address;//地址@property (nonatomic,copy)NSString *phoneNumber;//电话号码@property (nonatomic,copy)NSString *defaultAddress;//默认地址@property (nonatomic,copy)NSString *addressDetail;//详细地址@property (nonatomic,copy)NSString *num;//邮编@end
#import "AddressModel.h"@implementation AddressModel- (instancetype)initWithDict:(NSDictionary *)dict {    self = [super initWithDict:dict];    if (self) {        self.name = dict[@"name"];        self.address = dict[@"address"];        self.phoneNumber = dict[@"phoneNumber"];        self.defaultAddress = dict[@"defaultAddress"];        self.addressDetail = dict[@"addressDetail"];        self.num = dict[@"num"];    }    return self;}@end

注意:上面字典的键要和传入字典的键相同

使用:

1.添加数据:(假数据)

[self initDataSource:@[@{
@"name":@"黄智擒", @"phoneNumber":@"123456789", @"address":@"时代荆轲名媛 17-1102", @"defaultAddress":@"0", @"addressDetail":@"addressDetail", @"num":@"987654321"}, @{
@"name":@"黄智擒", @"phoneNumber":@"123456789", @"address":@"时代荆轲名媛 17-1102", @"defaultAddress":@"1", @"addressDetail":@"addressDetail", @"num":@"987654321"}, @{
@"name":@"黄智擒", @"phoneNumber":@"123456789", @"address":@"时代荆轲名媛 17-1102", @"defaultAddress":@"0", @"addressDetail":@"addressDetail", @"num":@"987654321"}, @{
@"name":@"黄智擒", @"phoneNumber":@"123456789", @"address":@"时代荆轲名媛 17-1102", @"defaultAddress":@"0", @"addressDetail":@"addressDetail", @"num":@"987654321"}, @{
@"name":@"黄智擒", @"phoneNumber":@"123456789", @"address":@"时代荆轲名媛 17-1102", @"defaultAddress":@"0", @"addressDetail":@"addressDetail", @"num":@"987654321"}, @{
@"name":@"黄智擒", @"phoneNumber":@"123456789", @"address":@"时代荆轲名媛 17-1102", @"defaultAddress":@"0", @"addressDetail":@"addressDetail", @"num":@"987654321"}]];

 

2.将模型添加到可变数组上(只有一个模型的话就用字典算了)

#pragma mark - 模型数据- (void)initDataSource:(NSArray *)dicArray {    _dataSource = [[NSMutableArray alloc]init];for (NSDictionary *dic in dicArray) {                AddressModel *model = [AddressModel modelWithDict:dic];        [_dataSource addObject:model];    }}

3.在UITableView中使用(补充使用)

cell中申明模型属性

@property (nonatomic,strong)AddressModel  *model;

重写set方法,添加模型数据到相应的控件上显示

-(void)setModel:(AddressModel *)model{    _model = model;    _nameAndNum.text = [NSString stringWithFormat:@"%@ %@",model.name,model.phoneNumber];    _address.text = [NSString stringWithFormat:@"%@ %@",model.address,model.addressDetail];}

效果图:

 

转载于:https://www.cnblogs.com/hxwj/p/4549973.html

你可能感兴趣的文章
如何学好C和C++
查看>>
Gitlab通过custom_hooks自动更新服务器代码
查看>>
python 如何判断调用系统命令是否执行成功
查看>>
Lesson10 vSphere 管理特性
查看>>
memcache 扩展和 memcached扩展安装
查看>>
好程序员的查克拉---自信
查看>>
线程池的设计(二):领导者追随者线程池的设计
查看>>
获取设备列表
查看>>
Django使用网上模板做个能展示的博客
查看>>
基于同IP不同端口,同端口不同Ip的虚拟主机 基于FQDN的虚拟主机
查看>>
项目软件集成三方模块,编译中int32和uint32定义冲突解决方法
查看>>
StretchDIBits速度测试(HALFTONE)
查看>>
在.NET Workflo“.NET研究”w 3.5中使用多线程提高工作流性能
查看>>
验证Oracle处理速度
查看>>
自己写一个jquery
查看>>
艾伟:C#中抽象类和接口的区别
查看>>
Flink - NetworkEnvironment
查看>>
BZOJ4374 : Little Elephant and Boxes
查看>>
【.Net Framework 体积大?】不安装.net framework 也能运行!?开篇叙述-1
查看>>
LLDP协议、STP协议 笔记
查看>>