亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 學院 > 開發設計 > 正文

ios開發-UI-UITableView及一個小實例

2019-11-14 18:57:38
字體:
來源:轉載
供稿:網友

  [注意]轉載時請注明出處博客園-吃唐僧肉的小悟空http://www.49028c.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

 

 

 


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
国产精品久久久av久久久| 亚洲成人久久一区| 亚洲xxxxx| 亚洲国产精品va在线看黑人| 91国在线精品国内播放| 91精品视频大全| 欧美激情aaaa| 久久婷婷国产麻豆91天堂| 疯狂做受xxxx欧美肥白少妇| 国产一区二区香蕉| 久久精品亚洲94久久精品| 国产精品爽爽ⅴa在线观看| 欧美日本高清视频| 精品国产成人在线| 欧美疯狂做受xxxx高潮| 国内精品久久久久影院 日本资源| 中文字幕亚洲字幕| 亚洲无亚洲人成网站77777| 欧美美女15p| 狠狠躁夜夜躁人人爽天天天天97| 欧美日韩福利在线观看| 91牛牛免费视频| 欧美激情在线视频二区| 亚洲人成网站777色婷婷| 尤物九九久久国产精品的分类| 国产精品免费看久久久香蕉| 久久久久久有精品国产| 久久久国产精彩视频美女艺术照福利| 久久久噜噜噜久久中文字免| 97视频在线观看成人| 久久全球大尺度高清视频| 97av在线视频免费播放| 韩国福利视频一区| 亚洲级视频在线观看免费1级| 中文字幕精品视频| 日韩av日韩在线观看| 国产高清视频一区三区| 久久久极品av| 中文字幕亚洲欧美日韩高清| 欧美激情视频免费观看| 88国产精品欧美一区二区三区| www国产精品com| 91精品国产色综合久久不卡98口| 91伊人影院在线播放| 欧美午夜性色大片在线观看| 国产福利精品av综合导导航| 国产精品夜色7777狼人| 欧美黑人巨大xxx极品| 亚洲人av在线影院| 国产一区二区三区日韩欧美| 国产亚洲欧洲高清一区| 91av在线播放| 欧美亚洲伦理www| 国产性猛交xxxx免费看久久| 日韩视频精品在线| 成人久久18免费网站图片| 国产欧美在线播放| 亚洲人成伊人成综合网久久久| 97在线观看视频国产| 亚洲自拍偷拍福利| 国产视频精品在线| 亚洲精品黄网在线观看| 亚洲精品永久免费| 国产精品久久久久久五月尺| 亚洲精品成人久久电影| 日本欧美中文字幕| 中文字幕欧美在线| 国产成人精品999| 亚洲一区二区三区香蕉| 日韩精品视频免费| 91国内产香蕉| 欧美成人精品激情在线观看| 欧美成人精品在线| 日韩激情在线视频| 日韩av免费在线| 国产91精品久久久| 成人黄色在线免费| 在线色欧美三级视频| 国产精品视频yy9099| 在线看国产精品| 欧美精品免费在线观看| 另类天堂视频在线观看| 久久精品亚洲国产| 亚洲女人初尝黑人巨大| 日韩精品免费观看| 宅男66日本亚洲欧美视频| 久久精品国产亚洲7777| 久久久久久一区二区三区| 国产精品露脸av在线| 久久久久久久久电影| 日韩欧美在线网址| 亚洲精选中文字幕| 一区二区三区四区在线观看视频| 欧美成人黑人xx视频免费观看| 欧美视频第一页| 日韩成人av在线播放| 亚洲曰本av电影| 在线亚洲国产精品网| 中文字幕亚洲自拍| 久久久久久久av| 国产精品高潮呻吟久久av黑人| 欧美成人午夜免费视在线看片| 久久久久久18| 国产97在线播放| 国产精品美乳一区二区免费| 亚洲免费福利视频| 久久久久久久久久久久久久久久久久av| 欧美疯狂xxxx大交乱88av| 久久免费视频观看| 亚洲国产精久久久久久| 国产视频精品xxxx| 亚洲欧洲日本专区| 91色在线观看| 丝袜亚洲另类欧美重口| 亚洲欧洲av一区二区| 欧美性高跟鞋xxxxhd| 欧美视频专区一二在线观看| 国内成人精品视频| 一区二区三区高清国产| 欧美精品一二区| 欧美一级视频免费在线观看| 91精品国产综合久久香蕉的用户体验| 国产精品久久久久久久7电影| 97视频在线观看亚洲| 欧美成人免费在线观看| 91丝袜美腿美女视频网站| 亚洲第一色在线| 亚洲码在线观看| 18性欧美xxxⅹ性满足| 国产精品久久一| 国产在线999| 国产成人精品一区二区| 日韩中文有码在线视频| 欧美大片免费观看| 日韩av在线最新| 亚洲色图13p| 欧美高清不卡在线| 国产精品香蕉在线观看| 91精品啪在线观看麻豆免费| 亚洲最新中文字幕| 中国china体内裑精亚洲片| 久久久久久国产精品美女| 欧美美最猛性xxxxxx| 亚洲精品按摩视频| 亚洲成人av在线播放| 色香阁99久久精品久久久| 亚洲美女免费精品视频在线观看| 久久人人爽人人爽人人片av高请| 亚洲精品中文字| 日本亚洲精品在线观看| 欧美日韩一区二区精品| www国产精品视频| 一区二区亚洲欧洲国产日韩| 国模精品视频一区二区| 国产欧美一区二区三区四区| 午夜精品一区二区三区在线视| 热99精品里视频精品| 日韩成人av网| 国产在线视频不卡| 亚洲日本中文字幕| 成人精品久久一区二区三区| 日韩精品福利在线| 国产精品久久久999| 国产成人精品亚洲精品|