上次介紹的UITableView,這里再做一個UITableView的小程序,汽車品牌,截圖如下:

1.1創(chuàng)建項目,這里不多講。
1.2 把所有汽車品牌的圖片放到images.xcassets中,如下圖:

1.3創(chuàng)建 plist數(shù)據(jù),plist數(shù)據(jù)里面每個array為一個汽車品牌分組,每個array里面又有一個array,這里面存放每個分組下所有的品牌汽車數(shù)據(jù),數(shù)據(jù)如下圖。

1.4數(shù)據(jù)創(chuàng)建完之后,然后設(shè)計頁面,頁面很簡單,直接放一個UItable View就可以了。
2.1后臺代碼,第一步導(dǎo)入
<UITableViewDataSource,UITableViewDelegate,UIAlertViewDelegate>
只有導(dǎo)入這UItable View的這幾個代理,我們才能在后面的代碼中使用UItable View的一些相對應(yīng)的方法。
2.2 創(chuàng)建UItable View控件的屬性,和創(chuàng)建一個存儲數(shù)據(jù)的數(shù)組,如下。
@PRoperty (weak, nonatomic) IBOutlet UITableView *tableView;
@property(nonatomic,strong)NSArray *carGroups;
2.3 加載數(shù)據(jù),這邊先要創(chuàng)建兩個模型類來保存數(shù)據(jù),國為我們這里的數(shù)據(jù)都在本地的plist文化中,所以我們要把這個plist里面的數(shù)據(jù)讀取出來保存在
創(chuàng)建的carGroups數(shù)組中,而本地的plist文件是一個array類型,而每個array里面又有一個array數(shù)組,所以我們要創(chuàng)建兩個模型類來保存數(shù)據(jù),一個模型類保存外面的array數(shù)據(jù),一個模型類來保存array里面的子array數(shù)據(jù),然后在模型類里面創(chuàng)建和plist里面對應(yīng)的數(shù)據(jù)的屬性和方法 代碼如下:

#import <Foundation/Foundation.h>@interface ZKCarModel : NSObject//頭像@property(nonatomic,copy)NSString * icon;//名字@property(nonatomic,copy)NSString *name;+(instancetype)CarWithDict:(NSDictionary *)dic;-(instancetype)initWithDict:(NSDictionary *)dic;@end#import "ZKCarModel.h"@implementation ZKCarModel-(instancetype)initWithDict:(NSDictionary *)dic{ if(self=[super init]) { [self setValuesForKeysWithDictionary:dic]; } return self;}+(instancetype)CarWithDict:(NSDictionary *)dic{ return [[self alloc] initWithDict:dic];}@end#import <Foundation/Foundation.h>#import "ZKCarModel.h"@interface ZKCarGroupModel : NSObject//題目@property(nonatomic,copy)NSString *title;@property(nonatomic,strong)NSArray *cars;+(instancetype)CarGroupWithDic:(NSDictionary *)dic;-(instancetype)initWithDict:(NSDictionary *)dic;@end#import "ZKCarGroupModel.h"@implementation ZKCarGroupModel-(instancetype)initWithDict:(NSDictionary *)dic{ if(self=[super init]) { self.title=dic[@"title"]; NSMutableArray *Array=[NSMutableArray array]; for (NSDictionary *dict in dic[@"cars"]) { ZKCarModel *Car=[ZKCarModel CarWithDict:dict]; [Array addObject:Car]; } self.cars=Array; } return self;}+(instancetype)CarGroupWithDic:(NSDictionary *)dic{ return [[self alloc] initWithDict:dic];}@end
2.4,對應(yīng)數(shù)據(jù)的模型類創(chuàng)建好以后,開始創(chuàng)建數(shù)組懶加載,代碼如下:

//加載數(shù)據(jù)-(NSArray *)carGroups{ if (_carGroups == nil) { // 1.獲得最主要的資源包 NSBundle *bundle = [NSBundle mainBundle]; // 2.獲得plist文件的全路徑 NSString *fullPath = [bundle pathForResource:@"cars_total.plist" ofType:nil]; // 3.根據(jù)全路徑加載數(shù)據(jù) NSArray *dictArray = [NSArray arrayWithContentsOfFile:fullPath]; // 4.將字典轉(zhuǎn)換為模型 // 專門用來保持模型(對象) NSMutableArray *models = [NSMutableArray arrayWithCapacity:dictArray.count]; // 遍歷字典數(shù)組 for (NSDictionary *dict in dictArray) { ZKCarGroupModel *app = [ZKCarGroupModel CarGroupWithDic:dict]; [models addObject:app]; } //賦值 _carGroups = models; } return _carGroups;}
2.5,數(shù)據(jù)加載完以后,然后就要開始寫UItable View中相對應(yīng)的代理方法了,代碼如下:

//設(shè)置分區(qū)-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return self.carGroups.count;}//設(shè)置每個分區(qū)顯示多少行數(shù)據(jù)-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ ZKCarGroupModel *Model=self.carGroups[section]; return Model.cars.count; }//每行顯示的數(shù)據(jù)-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *ID=@"A"; //從緩存中讀取cell UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:ID]; //如果緩存中沒有cell,創(chuàng)建一個新的cell if(cell==nil){ cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID]; } //找到當(dāng)前分區(qū)的索引 ZKCarGroupModel *GroupModel=self.carGroups[indexPath.section]; //找到當(dāng)前分區(qū)的行 ZKCarModel *CarModel=GroupModel.cars[indexPath.row]; //設(shè)置cell顯示的文字 cell.textLabel.text=CarModel.name; //設(shè)置cell顯示的圖片 cell.imageView.image=[UIImage imageNamed:CarModel.icon]; return cell;}
上面3個代理方法是UItable View中最常用的3個方法。寫完這3個方法運行xcode就可以看到數(shù)據(jù)了。
但這里還有些小問題,這里顯示的所有品牌都是從上往下排的,沒有一個分組,這樣我們想找哪個品牌的汽車并不太好找,所以,我們要把同一個數(shù)據(jù)
的汽車品牌加一個字母表示,這怎么做呢,這就要給UItable View的每個分區(qū)加一個頭了,使用titleForHeaderInSection代理方法,代碼如下:
//設(shè)置頭樣式-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{ //找到當(dāng)前分區(qū)在數(shù)組中的索引 ZKCarGroupModel *Model=self.carGroups[section]; //返回當(dāng)前分區(qū)的數(shù)據(jù)中的title return Model.title;}
2.6上面的程序中,在屏幕的最右邊還有一個索引,點這個索引就找找到相對應(yīng)的分區(qū)數(shù)據(jù),其實這個也很簡單,也是調(diào)用一個
sectionIndexTitlesForTableView的代理方法,這個方法返回一個array的數(shù)組。代碼如下:
//設(shè)置索引-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{ return [self.carGroups valueForKeyPath:@"title"];}
2.7,這個程序中還做了一個,當(dāng)你點擊屏幕上每個汽車品牌的時候還會彈出一個對話框,為什么要做這個呢,因為很多時候屏幕上的圖片和文字都是可以點擊的,所以光做一個靜態(tài)顯示好不是很好,雖然這個對話框好像并沒有什么用,但這里只是講下這個方法的使用,代碼如下。
//點擊cell時變化-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ //創(chuàng)建對話框 UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:@"汽車" message:@"取消" delegate:self cancelButtonTitle:@"確認(rèn)" otherButtonTitles:@"取消", nil]; //設(shè)置樣式 alertView.tag=1; alertView.alertViewStyle=UITableViewCellStyleSubtitle; //[alertView ]; [alertView show];}
3.1 一個UITableView做的汽車品牌就這樣OK了,雖然這并不是一個APP但,這里已經(jīng)把UITableView的一些常用代理方法都寫到了,當(dāng)然UITableView還有很多代表方法,這里并沒有講,但會了這些以后,在以后的使用中我們可以再來查詢,重要的是思想。
新聞熱點
疑難解答