标签:代理传值 scrollview mvc ios开发 项目实战
封装 封装 封装 。。。
封装的重要性太重要了 给大家在送点干货
从一个项目中抽取出来的,和大家一起分享 封装scrollView 循环滚动,tableViewCell(连载)
明天还会更新 tableView 的封装
使用了mvc 设计模式
代码如下:
// // GPMainController.m #import "GPMainController.h" #import "GPAdsView.h" #import "GPSubViewCell.h" #import "GPSubject.h" @interface GPMainController ()<GPAdsViewDelegate,UITableViewDataSource,UITableViewDelegate> @property (weak, nonatomic) IBOutlet UITableView *tableView; @property(nonatomic,strong)NSArray *plist; @property(nonatomic,strong)NSArray *subjects; @end @implementation GPMainController /** * 懒加载 */ -(NSArray *)plist { if (_plist == nil) { //路径 NSString *path = [[NSBundle mainBundle]pathForResource:@"quanquan.plist" ofType:nil]; NSArray *arrays = [NSArray arrayWithContentsOfFile:path]; _plist = arrays; NSLog(@"%@",_plist); } return _plist; } -(NSArray *)subjects { if (_subjects == nil) { NSMutableArray *marray = [[NSMutableArray alloc]init]; NSArray *dicts = self.plist[1]; for(NSDictionary *dic in dicts) { GPSubject *sub = [GPSubject SubjectWithDict:dic]; [marray addObject:sub]; } //NSLog(@"%@",marray); _subjects = marray; } NSLog(@"%@",self.plist); return _subjects; } - (void)viewDidLoad { [super viewDidLoad]; #warning 新增代码 GPAdsView *adsView = [GPAdsView adsView]; [self.view addSubview:adsView]; adsView.images = @[@"ad_00",@"ad_01",@"ad_02",@"ad_03",@"ad_04"]; //adsView.delegate = self; [adsView setAdsViewDidSelectedBlock:^(GPAdsView * adsView, NSString *image, NSInteger index) { [self adsViewDidSelected:adsView andImage:image andIndex:index]; }]; self.tableView.tableHeaderView = adsView; self.tableView.rowHeight = 120.f; } #pragma mark GPAdsViewDelegate 代理方法实现 -(void)adsViewDidSelected:(GPAdsView *)adsView andImage:(NSString *)image andIndex:(NSInteger)index { NSLog(@"图片名称:%@,索引值 是%ld",image,index); } //隐藏状态栏 -(BOOL)prefersStatusBarHidden { return YES; } -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.subjects.count; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { GPSubject *subject = self.subjects[indexPath.row]; GPSubViewCell *cell = [GPSubViewCell subjectCellWithTabelView:tableView]; cell.noteLabel.text = subject.note; cell.titleLabel.text = subject.title; cell.cardNumberLabel.text = subject.cardNumber; cell.iconImageView.image = [UIImage imageNamed:subject.icon]; //cell.subject = self.subjects[indexPath.row]; return cell; } @end
// // GPMainController.h #import <UIKit/UIKit.h> @interface GPMainController : UIViewController @end
// // GPSubViewCell.h #import <UIKit/UIKit.h> #import "GPSubject.h" @interface GPSubViewCell : UITableViewCell @property(nonatomic,strong)GPSubject *subject; @property (weak, nonatomic) IBOutlet UIImageView *iconImageView; @property (weak, nonatomic) IBOutlet UILabel *titleLabel; @property (weak, nonatomic) IBOutlet UILabel *cardNumberLabel; @property (weak, nonatomic) IBOutlet UILabel *noteLabel; +(id)subViewCell; +(id)subjectCellWithTabelView:(UITableView *)tableView; @end
// // GPSubViewCell.m #import "GPSubViewCell.h" @interface GPSubViewCell() @end @implementation GPSubViewCell +(id)subViewCell { //return [[[NSBundle mainBundle]loadNibNamed:NSStringFromClass([self class]) owner:nil options:nil]lastObject]; UINib *nib = [UINib nibWithNibName:NSStringFromClass([self class]) bundle:nil]; return [[nib instantiateWithOwner:nil options:nil]lastObject]; } +(id)subjectCellWithTabelView:(UITableView *)tableView { /* static NSString *cellName = @"cell"; GPSubViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellName]; if (cell == nil) { cell = [GPSubViewCell subViewCell]; } return cell; */ NSString * Identifier = NSStringFromClass([self class]); UINib *nib = [UINib nibWithNibName:Identifier bundle:nil]; [tableView registerNib:nib forCellReuseIdentifier:Identifier]; return [tableView dequeueReusableCellWithIdentifier:Identifier]; } -(void)setSubject:(GPSubject *)subject { _subject = subject; //更新子控件的数据 self.noteLabel.text = subject.note; self.cardNumberLabel.text = subject.cardNumber; self.titleLabel.text = subject.title; self.iconImageView.image = [UIImage imageNamed:subject.icon]; } @end
// // GPSubject.h #import <Foundation/Foundation.h> @interface GPSubject : NSObject @property(nonatomic,copy)NSString *title; @property(nonatomic,copy)NSString *cardNumber; @property(nonatomic,copy)NSString *note; @property(nonatomic,copy)NSString *icon; +(id)SubjectWithDict:(NSDictionary *)dict; -(id)initWithDict:(NSDictionary *)dict; @end
// // GPSubject.m #import "GPSubject.h" @implementation GPSubject +(id)SubjectWithDict:(NSDictionary *)dict { return [[self alloc]initWithDict:dict]; } -(id)initWithDict:(NSDictionary *)dict { if (self=[super init]) {//注意! //把字典中的数据取出来,存储到模型的属性中 //1.直接取字典中对应的属性进行赋值 //缺点:如果属性很多的话,需要写很多的赋值语句 /* self.title = dict[@"title"]; self.note = dict[@"note"]; self.cardNumber = dict[@"cardNumber"]; self.icon = dict[@"icon"]; //2.使用setValue forKey 反射机制实现(runtime)写框架一定会用到反射 [self setValue:dict[@"title"] forKey:@"title"]; [self setValue:dict[@"note"] forKey:@"icon"]; [self setValue:dict[@"cardNumber"] forKey:@"cardNumber"]; [self setValue:dict[@"icon"] forKey:@"icon"]; //必须保证,字典中的key 与模型中的属性--对应 NSArray *keys = [dict allKeys]; for(NSString *key in keys) { [self setValue:[dict objectForKey:key] forKey:key]; } */ // [self setValuesForKeysWithDictionary:dict]; } return self; } -(NSString *)description { return [NSString stringWithFormat:@"title = %@,icon = %@",_title,_icon]; } @end
版权声明:本文为博主原创文章,未经博主允许不得转载。
封装scrollView 循环滚动,tableViewCell(连载) mvc
标签:代理传值 scrollview mvc ios开发 项目实战
原文地址:http://blog.csdn.net/u012701023/article/details/47686561