封裝 封裝 封裝 。。。設計模式
封裝的重要性過重要了 給你們在送點乾貨mvc
從一個項目中抽取出來的。和你們一塊兒分享 封裝scrollView 循環滾動。tableViewCell(連載) 框架
明天還會更新 tableView 的封裝 post
使用了mvc 設計模式atom
代碼例如如下:設計
// // 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