UITableView 實現(xiàn)汽車品牌(demo)
看TableView的資料其實已經(jīng)蠻久了,一直想寫點兒東西,卻總是因為各種原因拖延,今天晚上有時間靜下心來記錄一些最近學習的TableView的知識。下面進入正題,UITableView堪稱UIKit里面最復雜的一個控件了,使用起來不算難,但是要用好并不容易。當使用的時候我們必須要考慮到后臺數(shù)據(jù)的設計,tableViewCell的設計和重用以及tableView的效率等問題。
上次介紹的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)建完之后,然后設計頁面,頁面很簡單,直接放一個UItable View就可以了。
2.1后臺代碼,第一步導入
<UITableViewDataSource,UITableViewDelegate,UIAlertViewDelegate>
只有導入這UItable View的這幾個代理,我們才能在后面的代碼中使用UItable View的一些相對應的方法。
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里面對應的數(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,對應數(shù)據(jù)的模型類創(chuàng)建好以后,開始創(chuàng)建數(shù)組懶加載
代碼如下:
#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.5,數(shù)據(jù)加載完以后,然后就要開始寫UItable View中相對應的代理方法了
代碼如下:
//設置分區(qū) -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return self.carGroups.count; } //設置每個分區(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]; } //找到當前分區(qū)的索引 ZKCarGroupModel *GroupModel=self.carGroups[indexPath.section]; //找到當前分區(qū)的行 ZKCarModel *CarModel=GroupModel.cars[indexPath.row]; //設置cell顯示的文字 cell.textLabel.text=CarModel.name; //設置cell顯示的圖片 cell.imageView.image=[UIImage imageNamed:CarModel.icon]; return cell; }
上面3個代理方法是UItable View中最常用的3個方法。寫完這3個方法運行xcode就可以看到數(shù)據(jù)了。
但這里還有些小問題,這里顯示的所有品牌都是從上往下排的,沒有一個分組,這樣我們想找哪個品牌的汽車并不太好找,所以,我們要把同一個數(shù)據(jù)的汽車品牌加一個字母表示,這怎么做呢,這就要給UItable View的每個分區(qū)加一個頭了,使用titleForHeaderInSection代理方法
代碼如下:
//設置頭樣式 -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { //找到當前分區(qū)在數(shù)組中的索引 ZKCarGroupModel *Model=self.carGroups[section]; //返回當前分區(qū)的數(shù)據(jù)中的title return Model.title; }
2.6上面的程序中,在屏幕的最右邊還有一個索引,點這個索引就找找到相對應的分區(qū)數(shù)據(jù),其實這個也很簡單,也是調用一個
sectionIndexTitlesForTableView的代理方法,這個方法返回一個array的數(shù)組。
代碼如下:
//設置索引 -(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView { return [self.carGroups valueForKeyPath:@"title"]; }
2.7,這個程序中還做了一個,當你點擊屏幕上每個汽車品牌的時候還會彈出一個對話框,為什么要做這個呢,因為很多時候屏幕上的圖片和文字都是可以點擊的,所以光做一個靜態(tài)顯示好不是很好,雖然這個對話框好像并沒有什么用,但這里只是講下這個方法的使用
代碼如下:
//點擊cell時變化 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { //創(chuàng)建對話框 UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:@"汽車" message:@"取消" delegate:self cancelButtonTitle:@"確認" otherButtonTitles:@"取消", nil]; //設置樣式 alertView.tag=1; alertView.alertViewStyle=UITableViewCellStyleSubtitle; //[alertView ]; [alertView show]; }
3.1 一個UITableView做的汽車品牌就這樣OK了,雖然這并不是一個APP但,這里已經(jīng)把UITableView的一些常用代理方法都寫到了,當然UITableView還有很多代表方法,這里并沒有講,但會了這些以后,在以后的使用中我們可以再來查詢,重要的是思想。
以上是UITableView 實現(xiàn)汽車品牌的全部內(nèi)容,希望對大家有所幫助。
- IOS UITableView和UITableViewCell的幾種樣式詳細介紹
- iOS應用開發(fā)中UITableView的分割線的一些設置技巧
- 改變iOS應用中UITableView的背景顏色與背景圖片的方法
- iOS App中UITableView左滑出現(xiàn)刪除按鈕及其cell的重用
- IOS中UITableView滾動到指定位置
- iOS應用中UITableView左滑自定義選項及批量刪除的實現(xiàn)
- 詳解iOS開發(fā)中UItableview控件的數(shù)據(jù)刷新功能的實現(xiàn)
- iOS中UITableView Cell實現(xiàn)自定義單選功能
- iOS開發(fā)之UITableView左滑刪除等自定義功能
- iOS中PNChart與UITableView的聯(lián)動示例詳解
相關文章
Objective-C的NSOperation多線程類基本使用指南
這篇文章主要介紹了Objective-C的NSOperation多線程類基本使用指南,談到了Operations的執(zhí)行順序和并發(fā)量等設置操作,需要的朋友可以參考下2016-02-02IOS 自定義UICollectionView的頭視圖或者尾視圖UICollectionReusableView
這篇文章主要介紹了IOS 自定義UICollectionView的頭視圖或者尾視圖UICollectionReusableView的相關資料,需要的朋友可以參考下2017-01-01AVFoundation AVCaptureSession媒體捕捉
這篇文章主要為大家介紹了ios開發(fā)AVFoundation AVCaptureSession媒體捕捉詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-10-10