ios開發-UI-UITableView及一個小執行個體,ios-ui-uitableview

來源:互聯網
上載者:User

ios開發-UI-UITableView及一個小執行個體,ios-ui-uitableview

  [注意]轉載時請註明出處部落格園-吃唐僧肉的小悟空http://www.cnblogs.com/hukezhu/

  在IOS中,實現表格展示資料,最常用的做法就是使用UITableView.

@interface UITableView : UIScrollView <NSCoding>

  UITableView繼承自UIScrollView,所以支援垂直滾動,而且效能極佳(這個會在以後提到,支援重用cell)

  UITableView有兩種樣式:分組和不分組

    分組:UITableViewStyleGrouped

    不分組:UITableViewStylePlain

  

@property (nonatomic, readonly) UITableViewStyle           style;@property (nonatomic, assign)   id <UITableViewDataSource> dataSource;@property (nonatomic, assign)   id <UITableViewDelegate>   delegate;@property (nonatomic)          CGFloat                     rowHeight;            // will return the default value if unset
  如何展示資料

  1.UITableView需要一個資料來源(dataSource)來顯示資料(如上面代碼)

  2.UITableView會向資料來源查詢一共有多少行資料以及每一行顯示什麼資料等

    

@protocol UITableViewDataSource<NSObject>@required- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;@optional- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;              // Default is 1 if not implemented- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;    // fixed font style. use custom view (UILabel) if you want something different- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;// Editing// Individual rows can opt out of having the -editing property set for them. If not implemented, all rows are assumed to be editable.- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath;// Moving/reordering// Allows the reorder accessory view to optionally be shown for a particular row. By default, the reorder control will be shown only if the datasource implements -tableView:moveRowAtIndexPath:toIndexPath:- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath;// Index- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView;                                                    // return list of section titles to display in section index view (e.g. "ABCD...Z#")- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index;  // tell table which section corresponds to section title/index (e.g. "B",1))// Data manipulation - insert and delete support// After a row has the minus or plus button invoked (based on the UITableViewCellEditingStyle for the cell), the dataSource must commit the change// Not called for edit actions using UITableViewRowAction - the action's handler will be invoked instead- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath;// Data manipulation - reorder / moving support- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath;@end

  3.沒有設定資料元的UITableView只是一個空的殼子,不會展示任何資料

  4.上面代碼是UITableViewDataSource協議的,凡是遵守UITableViewDataSource協議的OC對象,都可以是UITableView的資料來源,此處我們一般選擇控制器為資料來源.

  所以總結一下使用UITableView展示資料的步驟:

    • 遵守UITableViewDataSource協議
    • 設定控制器為UITableView的資料來源(此處假設是控制器)
    • 實現資料來源的方法

  

 

  UITableViewDataSource協議中必須要實現的方法:

  

@required//每一組有多少行- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)//每一行顯示什麼樣的內容- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;

  

  下面這個方法不是必須要實現的,不實現預設就是1組

//一共有多少組- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;//這個方法不是必須要實現的,如果不實現,就預設是一組

 

  下面通過一個小例子來示範UITableView的使用

  

 

是應用代碼結構及plist檔案:

  

  此處的plist檔案相對於前面文章介紹的比較複雜,此處是字典中又包含字典,所以使用的字典轉模型的方法與之前也有所不同.首先要將裡面的字典先轉化為模型,再轉化外面的.

中標註的KZCar是汽車資料模型

中標註的KZCarGroup是汽車組的資料模型,下面附上代碼,其中尤其要注意,這個複雜的資料模型的轉化過程.

KZCar.h

 1 // 2 //  KZCar.h 3 //  UI基礎-06-05-19 4 // 5 //  Created by hukezhu on 15/5/20. 6 // 7 // 8  9 #import <Foundation/Foundation.h>10 11 @interface KZCar : NSObject12 13 /**14  *  聲明圖片屬性15  */16 @property (nonatomic,copy) NSString *icon;17 18 /**19  *  聲明名稱屬性20  */21 @property (nonatomic,copy) NSString *name;22 23 24 -(instancetype)initWithDict:(NSDictionary *)dict;25 26 +(instancetype)carWithDict:(NSDictionary *)dict;27 28 +(NSArray *)carsWithDict:(NSArray *)dictarray;29 @end

KZCar.m

 1 // 2 //  KZCar.m 3 //  UI基礎-06-05-19 4 // 5 //  Created by hukezhu on 15/5/20. 6 // 7 // 8  9 #import "KZCar.h"10 11 @implementation KZCar12 13 -(instancetype)initWithDict:(NSDictionary *)dict{14 15     if (self = [super init]) {16         //使用KVC17         [self setValuesForKeysWithDictionary:dict];18     }19     return  self;20 }21 22 23 +(instancetype)carWithDict:(NSDictionary *)dict{24 25     return [[self alloc]initWithDict:dict];26 }27 28 //此處是裡面的汽車資料模型,分析汽車數組轉化的時候,發現此處只需要傳入一個數組即可29 +(NSArray *)carsWithDict:(NSArray *)dictarray{30 31     32     NSMutableArray *tempArray = [NSMutableArray array];33     for (NSDictionary *dict in dictarray) {34         KZCar *car = [KZCar carWithDict:dict];35         [tempArray addObject:car];36     }37     return tempArray;38 }39 @end

 

KZCarGroup.h
////  KZCarGroup.h//  UI基礎-06-05-19////  Created by hukezhu on 15/5/20.////#import <Foundation/Foundation.h>@interface KZCarGroup : NSObject/** *  聲明一個汽車的屬性 */@property (nonatomic,strong)NSArray *cars;/** *  聲明一個標題的屬性 */@property (nonatomic,copy)NSString *title;-(instancetype)initWithDict:(NSDictionary *)dict;+(instancetype)carGroupWithDict:(NSDictionary *)dict;+(NSArray *)carGroups;@end
KZCarGroup.m
 1 // 2 //  KZCarGroup.m 3 //  UI基礎-06-05-19 4 // 5 //  Created by hukezhu on 15/5/20. 6 // 7 // 8  9 #import "KZCarGroup.h"10 #import "KZCar.h"11 12 @implementation KZCarGroup13 -(instancetype)initWithDict:(NSDictionary *)dict{14 15     if (self = [super init]) {16         _title = dict[@"title"];17         18         //將字典中的數組cars取出來19         NSArray *array = dict[@"cars"];20         _cars = [KZCar carsWithDict:array];21     }22     return self;23 }24 25 +(instancetype)carGroupWithDict:(NSDictionary *)dict{26 27     return [[self alloc]initWithDict:dict];28 }29 30 +(NSArray *)carGroups{31 32 33     NSString *path = [[NSBundle mainBundle]pathForResource:@"cars_total" ofType:@"plist"];34     NSArray *dictArray = [NSArray arrayWithContentsOfFile:path];35     36     NSMutableArray *tempArray = [NSMutableArray array];37     for (NSDictionary *dict in dictArray) {38         KZCarGroup *carGroup = [KZCarGroup carGroupWithDict:dict];39         [tempArray addObject:carGroup];40     }41     return tempArray;42 }43 @end

 

ViewController.m

  1 //  2 //  ViewController.m  3 //  05-car多資料  4 //  5 //  Created by hukezhu on 15/5/20.  6 //  7   8   9  10  11 #import "ViewController.h" 12 #import "KZCarGroup.h" 13 #import "KZCar.h" 14  15 @interface ViewController ()<UITableViewDataSource> 16  17 @property (weak, nonatomic) IBOutlet UITableView *tableView; 18  19  20  21 @property(nonatomic,strong)NSArray *cars; 22 @end 23  24 @implementation ViewController 25  26 - (void)viewDidLoad { 27     [super viewDidLoad]; 28      29     self.tableView.dataSource = self; 30     NSLog(@"%@",self.cars); 31  32 } 33 #pragma mark -資料來源方法 34 /** 35  *  返回一共有多少組 36  37  */ 38 -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ 39  40     return self.cars.count; 41 } 42  43 /** 44  *  返回每一組有多少行 45  * 46  */ 47 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 48  49     KZCarGroup *carGroup = self.cars[section]; 50     return carGroup.cars.count; 51 } 52  53 /** 54  *  返回每行顯示什麼樣的資料 55  * 56  */ 57 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 58  59     NSString *identity = @"yes"; 60     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identity ]; 61      62     if (cell == nil) { 63         cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identity]; 64     } 65     //取出組模型 66     KZCarGroup *carGroup = self.cars[indexPath.section]; 67     //取出行模型 68     KZCar *car = carGroup.cars[indexPath.row]; 69      70     cell.imageView.image = [UIImage imageNamed:car.icon]; 71     cell.textLabel.text = car.name; 72     return cell; 73 } 74  75  76 /** 77  *  設定頭部biaoti 78  * 79  */ 80 -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{ 81  82     //取出組模型 83     KZCarGroup *carGroup = self.cars[section]; 84     return carGroup.title; 85      86 } 87  88 /** 89  *  設定尾部描述 90  * 91  */ 92 -(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{ 93  94     //取出組模型 95     KZCarGroup *carGroup = self.cars[section]; 96     return carGroup.title; 97      98 } 99 /**100  *  懶載入資料101  *102  */103 -(NSArray *)cars{104 105     if (_cars == nil) {106         _cars = [KZCarGroup carGroups];107     }108     return _cars;109 }110 @end

 

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.