iOS開發UI篇—使用xib自定義UItableviewcell實現一個簡單的團購應用界面佈局架構
1、項目文件結構和plist文件app
2、實現效果ide
3、代碼示例佈局
1.沒有使用配套的類,而是直接使用xib文件控件tag值操做優化
數據模型部分:atom
YYtg.h文件spa
// // YYtg.h // 01-團購數據顯示(沒有配套的類) // // Created by apple on 14-5-29. // Copyright (c) 2014年 itcase. All rights reserved. // #import <Foundation/Foundation.h> #import "Global.h" @interface YYtg : NSObject @property(nonatomic,copy)NSString *buyCount; @property(nonatomic,copy)NSString *icon; @property(nonatomic,copy)NSString *price; @property(nonatomic,copy)NSString *title; YYinitH(tg) @end
YYtg.m文件3d
// // YYtg.m // 01-團購數據顯示(沒有配套的類) // // Created by apple on 14-5-29. // Copyright (c) 2014年 itcase. All rights reserved. // #import "YYtg.h" @implementation YYtg YYinitM(tg) @end
主控制器code
YYViewController.m文件orm
// // YYViewController.m // 01-團購數據顯示(沒有配套的類) // // Created by apple on 14-5-29. // Copyright (c) 2014年 itcase. All rights reserved. // #import "YYViewController.h" #import "YYtg.h" @interface YYViewController ()<UITableViewDataSource> @property(nonatomic,strong)NSArray *tg; @property (strong, nonatomic) IBOutlet UITableView *tableview; @end @implementation YYViewController - (void)viewDidLoad { [super viewDidLoad]; self.tableview.rowHeight=100; } #pragma mark- 懶加載 -(NSArray *)tg { if (_tg==nil) { NSString *fullpath=[[NSBundle mainBundle]pathForResource:@"tgs.plist" ofType:nil]; NSArray *temparray=[NSArray arrayWithContentsOfFile:fullpath]; NSMutableArray *arrayM=[NSMutableArray arrayWithCapacity:temparray.count]; for (NSDictionary *dict in temparray) { YYtg *tg=[YYtg tgWithDict:dict]; [arrayM addObject:tg]; } _tg=[arrayM mutableCopy]; } return _tg; } #pragma mark-數據顯示 -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.tg.count; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { //讀取xib中的數據 // NSArray *arrayM=[[NSBundle mainBundle]loadNibNamed:@"tgcell" owner:nil options:nil]; // UITableViewCell *cell=[arrayM firstObject]; static NSString *identifier=@"tg"; UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier]; if (cell==nil) { // cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]; cell= [[[NSBundle mainBundle]loadNibNamed:@"tgcell" owner:nil options:nil] firstObject]; } YYtg *tg=self.tg[indexPath.row]; //設置數據 //使用tag UIImageView *imgv=(UIImageView *)[cell viewWithTag:1]; imgv.image=[UIImage imageNamed:tg.icon]; UILabel *buyCount=(UILabel *)[cell viewWithTag:4]; buyCount.text=[NSString stringWithFormat:@"已有%@人購買",tg.buyCount]; UILabel *title=(UILabel *)[cell viewWithTag:2]; title.text=tg.title; UILabel *price=(UILabel *)[cell viewWithTag:3]; price.text=[NSString stringWithFormat:@"$%@",tg.price]; //返回cell return cell; } //隱藏狀態欄 -(BOOL)prefersStatusBarHidden { return YES; } @end
使用xib自定義的UItableviewcell
代碼分析:
上面的代碼經過使用xib文件中各個控件的tag值,完成對每一個部分數據的賦值和刷新。可是,做爲主控制器,它應該知道xib文件中各個控件的tag值,它知道的是否是太多了呢?
爲了解決上面的問題,咱們能夠爲自定義的cell設置一個配套的類,讓這個類來操做這個xib,對外提供接口,至於內部的數據處理,外界不須要關心,也不用關心。
改造後的代碼以下:
2.使用xib和對應的類完成自定義cell的數據展現
新建一個類,用來管理對應的xib文件
注意類的繼承類,並把該類和xib文件進行關聯
YYtgcell.h文件代碼:
// // YYtgcell.h // 02-團購(使用xib和類完成數據展現) // // Created by apple on 14-5-29. // Copyright (c) 2014年 itcase. All rights reserved. // #import <UIKit/UIKit.h> #import "YYtg.h" @interface YYtgcell : UITableViewCell @property(nonatomic,strong)YYtg *yytg; @end
YYtgcell.m文件
// // YYtgcell.m // 02-團購(使用xib和類完成數據展現) // // Created by apple on 14-5-29. // Copyright (c) 2014年 itcase. All rights reserved. // #import "YYtgcell.h" //私有擴展 @interface YYtgcell() @property (strong, nonatomic) IBOutlet UIImageView *img; @property (strong, nonatomic) IBOutlet UILabel *titlelab; @property (strong, nonatomic) IBOutlet UILabel *pricelab; @property (strong, nonatomic) IBOutlet UILabel *buycountlab; @end @implementation YYtgcell #pragma mark 重寫set方法,完成數據的賦值操做 -(void)setYytg:(YYtg *)yytg { _yytg=yytg; self.img.image=[UIImage imageNamed:yytg.icon]; self.titlelab.text=yytg.title; self.pricelab.text=[NSString stringWithFormat:@"$%@",yytg.price]; self.buycountlab.text=[NSString stringWithFormat:@"已有%@人購買",yytg.buyCount]; } @end
主控制器
YYViewController.m文件
// // YYViewController.m // 02-團購(使用xib和類完成數據展現) // // Created by apple on 14-5-29. // Copyright (c) 2014年 itcase. All rights reserved. // #import "YYViewController.h" #import "YYtg.h" #import "YYtgcell.h" @interface YYViewController ()<UITableViewDataSource,UITableViewDelegate> @property (strong, nonatomic) IBOutlet UITableView *tableview; @property(strong,nonatomic)NSArray *tg; @end @implementation YYViewController - (void)viewDidLoad { [super viewDidLoad]; self.tableview.rowHeight=80.f; } #pragma mark- 懶加載 -(NSArray *)tg { if (_tg==nil) { NSString *fullpath=[[NSBundle mainBundle]pathForResource:@"tgs.plist" ofType:nil]; NSArray *temparray=[NSArray arrayWithContentsOfFile:fullpath]; NSMutableArray *arrayM=[NSMutableArray arrayWithCapacity:temparray.count]; for (NSDictionary *dict in temparray) { YYtg *tg=[YYtg tgWithDict:dict]; [arrayM addObject:tg]; } _tg=[arrayM mutableCopy]; } return _tg; } #pragma mark- xib建立cell數據處理 #pragma mark 多少組 -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } #pragma mark多少行 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.tg.count; } #pragma mark設置每組每行 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *identifier= @"tg"; YYtgcell *cell=[tableView dequeueReusableCellWithIdentifier:identifier]; if (cell==nil) { //如何讓建立的cell加個戳 //對於加載的xib文件,能夠到xib視圖的屬性選擇器中進行設置 cell=[[[NSBundle mainBundle]loadNibNamed:@"tgcell" owner:nil options:nil]firstObject]; NSLog(@"建立了一個cell"); } //設置cell的數據 //獲取當前行的模型 YYtg *tg=self.tg[indexPath.row]; cell.yytg=tg; return cell; } -(BOOL)prefersStatusBarHidden { return YES; } @end
3.對上述代碼進行進一步的優化和調整(MVC)
優化以下:
(1)把主控制器中建立cell的過程抽取到YYtgcell中完成,並對外提供一個接口。
YYtgcell.h文件(提供接口)
#import <UIKit/UIKit.h> #import "YYtgModel.h" @interface YYtgcell : UITableViewCell @property(nonatomic,strong)YYtgModel *yytg; //把加載數據(使用xib建立cell的內部細節進行封裝) +(instancetype)tgcellWithTableView:(UITableView *)tableView; @end
YYtgcell.m文件(把建立自定義cell的部分進行封裝)
// // YYtgcell.m // 02-團購(使用xib和類完成數據展現) // // Created by apple on 14-5-29. // Copyright (c) 2014年 itcase. All rights reserved. // #import "YYtgcell.h" //私有擴展 @interface YYtgcell() @property (strong, nonatomic) IBOutlet UIImageView *img; @property (strong, nonatomic) IBOutlet UILabel *titlelab; @property (strong, nonatomic) IBOutlet UILabel *pricelab; @property (strong, nonatomic) IBOutlet UILabel *buycountlab; @end @implementation YYtgcell #pragma mark 重寫set方法,完成數據的賦值操做 -(void)setYytg:(YYtgModel *)yytg { _yytg=yytg; self.img.image=[UIImage imageNamed:yytg.icon]; self.titlelab.text=yytg.title; self.pricelab.text=[NSString stringWithFormat:@"$%@",yytg.price]; self.buycountlab.text=[NSString stringWithFormat:@"已有%@人購買",yytg.buyCount]; } +(instancetype)tgcellWithTableView:(UITableView *)tableView { static NSString *identifier= @"tg"; YYtgcell *cell=[tableView dequeueReusableCellWithIdentifier:identifier]; if (cell==nil) { //如何讓建立的cell加個戳 //對於加載的xib文件,能夠到xib視圖的屬性選擇器中進行設置 cell=[[[NSBundle mainBundle]loadNibNamed:@"tgcell" owner:nil options:nil]firstObject]; NSLog(@"建立了一個cell"); } return cell; } @end
主控器中的業務邏輯更加清晰,YYViewController.m文件代碼以下
// // YYViewController.m // 02-團購(使用xib和類完成數據展現) // // Created by apple on 14-5-29. // Copyright (c) 2014年 itcase. All rights reserved. // #import "YYViewController.h" #import "YYtgModel.h" #import "YYtgcell.h" @interface YYViewController ()<UITableViewDataSource,UITableViewDelegate> @property (strong, nonatomic) IBOutlet UITableView *tableview; @property(strong,nonatomic)NSArray *tg; @end @implementation YYViewController - (void)viewDidLoad { [super viewDidLoad]; self.tableview.rowHeight=80.f; } #pragma mark- 懶加載 -(NSArray *)tg { if (_tg==nil) { NSString *fullpath=[[NSBundle mainBundle]pathForResource:@"tgs.plist" ofType:nil]; NSArray *temparray=[NSArray arrayWithContentsOfFile:fullpath]; NSMutableArray *arrayM=[NSMutableArray arrayWithCapacity:temparray.count]; for (NSDictionary *dict in temparray) { YYtgModel *tg=[YYtgModel tgWithDict:dict]; [arrayM addObject:tg]; } _tg=[arrayM mutableCopy]; } return _tg; } #pragma mark- xib建立cell數據處理 #pragma mark 多少組 -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } #pragma mark多少行 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.tg.count; } #pragma mark設置每組每行 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { //1.建立cell YYtgcell *cell=[YYtgcell tgcellWithTableView:tableView]; //2.獲取當前行的模型,設置cell的數據 YYtgModel *tg=self.tg[indexPath.row]; cell.yytg=tg; //3.返回cell return cell; } #pragma mark- 隱藏狀態欄 -(BOOL)prefersStatusBarHidden { return YES; } @end
4、推薦調整的項目文件結構
這是調整後的文件結構,完整的MVC架構。
注意:注意文件的命名規範。
提示技巧:批量更名,操做以下:
修改成想要的名稱: