UITableView 繼承自 UIScrollView ,用於實現表格數據展現,支持垂直滾動。html
UITableView 須要一個數據源來顯示數據,並向數據源查詢一共有多少行數據以及每一行顯示什麼內容等。凡是遵照 UITableViewDataSource 協議的Objc對象,均可以是 UITableView 的數據源。atom
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 返回共有多少組數據。spa
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 返回每一組有多少行數據。code
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 返回每一行顯示的內容。htm
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section 返回各組底部顯示的標題。對象
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 返回各組頭部顯示的標題。blog
新建一個Single View Application,命名爲TableViewDemo,首先讓 ViewController 類遵照 UITableViewDataSource 協議,並聲明一個 UITableView 屬性,以下所示:繼承
1 //ViewController.m 2 @interface ViewController () <UITableViewDataSource> 3 @property (weak, nonatomic) IBOutlet UITableView *tableView; 4 @end
將Table View視圖拖到storyboard中,並將其與 tableView 屬性創建關聯。開發
接下來設置 tableView 的數據源,修改 viewDidLoad 方法:get
1 //ViewController.m 2 - (void)viewDidLoad { 3 [super viewDidLoad]; 4 self.tableView.dataSource = self; 5 }
重寫 UITableViewDataSource 協議的方法:
1 #pragma mark - UITableViewDataSource 2 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 3 NSLog(@"%s", __FUNCTION__); 4 return 2; 5 } 6 7 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 8 NSLog(@"%s", __FUNCTION__); 9 if (section == 0) { 10 return 2; 11 } else { 12 return 3; 13 } 14 } 15 16 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 17 NSLog(@"%s %d %d", __FUNCTION__, indexPath.section, indexPath.row); 18 UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil]; 19 if (indexPath.section == 0) { 20 if (indexPath.row == 0) { 21 cell.textLabel.text = @"奧迪"; 22 } else if (indexPath.row == 1) { 23 cell.textLabel.text = @"寶馬"; 24 } 25 } else if (indexPath.section == 1) { 26 if (indexPath.row == 0) { 27 cell.textLabel.text = @"本田"; 28 } else if (indexPath.row == 1) { 29 cell.textLabel.text = @"豐田"; 30 } else if (indexPath.row == 2) { 31 cell.textLabel.text = @"馬自達"; 32 } 33 } 34 return cell; 35 } 36 37 - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section { 38 if (section == 0) { 39 return @"高端大氣上檔次"; 40 } else { 41 return @"還不錯"; 42 } 43 } 44 45 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { 46 if (section == 0) { 47 return @"德系品牌"; 48 } else { 49 return @"日韓品牌"; 50 } 51 }
UITableViewCell 有一個 UITableViewCellStyle 屬性,用於以爲其 contentView 使用哪些子類型,該屬性定義以下:
1 typedef enum : NSInteger { 2 UITableViewCellStyleDefault, 3 UITableViewCellStyleValue1, 4 UITableViewCellStyleValue2, 5 UITableViewCellStyleSubtitle 6 } UITableViewCellStyle;
爲顯示的內容按組創建數據模型類,命名爲 WJQCarGroup ,聲明下列屬性:
1 //WJQCarGroup.h 2 @interface WJQCarGroup : NSObject 3 @property (nonatomic, copy) NSString *title; 4 @property (nonatomic, copy) NSString *desc; 5 @property (nonatomic, strong) NSArray *cars; //當前組全部行的數據 6 @end
接下來在 ViewController.m 文件中導入 WJQCarGroup.h ,並在類擴展中聲明 NSArray 屬性用來存放全部組的數據,以下:
1 //ViewController.m 2 @interface ViewController () <UITableViewDataSource> 3 @property (weak, nonatomic) IBOutlet UITableView *tableView; 4 @property (nonatomic, strong) NSArray *carGroups; //保存全部組的數據 5 @end
使用懶加載技術重寫 carGroups 屬性的 getter 方法,以下:
1 //ViewController.m 2 #pragma mark - 懶加載 3 - (NSArray *)carGroups { 4 if (_carGroups == nil) { 5 WJQCarGroup *cg1 = [[WJQCarGroup alloc] init]; 6 cg1.title = @"德系品牌"; 7 cg1.desc = @"高端大氣上檔次"; 8 cg1.cars = @[@"奧迪", @"寶馬"]; 9 10 WJQCarGroup *cg2 = [[WJQCarGroup alloc] init]; 11 cg2.title = @"日韓系列"; 12 cg2.desc = @"還不錯"; 13 cg2.cars = @[@"本田", @"豐田"]; 14 15 WJQCarGroup *cg3 = [[WJQCarGroup alloc] init]; 16 cg3.title = @"歐美品牌"; 17 cg3.desc = @"價格昂貴"; 18 cg3.cars = @[@"勞斯萊斯", @"布加迪", @"小米"]; 19 _carGroups = @[cg1, cg2, cg3]; 20 } 21 return _carGroups; 22 }
最後,修改 UITableViewDataSource 協議的相關方法:
1 //ViewController.m 2 #pragma mark - UITableViewDataSource 3 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 4 NSLog(@"%s", __FUNCTION__); 5 return self.carGroups.count; 6 } 7 8 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 9 NSLog(@"%s", __FUNCTION__); 10 WJQCarGroup *carGroup = self.carGroups[section]; 11 return carGroup.cars.count; 12 } 13 14 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 15 NSLog(@"%s %d %d", __FUNCTION__, indexPath.section, indexPath.row); 16 UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil]; 17 WJQCarGroup *carGroup = self.carGroups[indexPath.section]; 18 NSString *carName = carGroup.cars[indexPath.row]; 19 cell.textLabel.text = carName; 20 return cell; 21 } 22 23 - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section { 24 WJQCarGroup *carGroup = self.carGroups[section]; 25 return carGroup.desc; 26 } 27 28 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { 29 WJQCarGroup *carGroup = self.carGroups[section]; 30 return carGroup.title; 31 }
參考博客:iOS開發UI篇—UITableview控件簡單介紹
實例代碼:http://vdisk.weibo.com/s/DiY98QyXCOne0
1 //ViewController.m 2 #pragma mark - 控制狀態欄是否顯示 3 - (BOOL)prefersStatusBarHidden { 4 //返回YES表明隱藏狀態欄,NO相反 5 return YES; 6 }