記錄一個菜鳥的iOS學習之旅,如能幫助正在學習的你,亦楓不勝榮幸;如路過的大神如指教幾句,亦楓感激涕淋!
在iOS中使用表視圖UITableView,最重要的是使用好表視圖的兩個協(xié)議: UITableViewDataSource
數(shù)據(jù)源協(xié)議和 UITableViewDelegate
委托協(xié)議。靈活使用這兩個協(xié)議便可以自由定義表視圖的內容數(shù)據(jù)和單元格樣式。
在這篇文章中,我們從使用iOS系統(tǒng)api提供的單元格到自定義單元格樣式創(chuàng)建簡單的表視圖,學習UITableView的使用,同時掌握各種其他iOS基礎知識。
使用 Simple View Application
模板創(chuàng)建一個工程,由于這里我們使用的是表視圖控制器,而系統(tǒng)生成的模板是普通視圖控制器,所以我們需要在故事板文件的 View Controller Scene
中刪除View Controller,在控件庫中重新拖拽一個Table View Controller到設計界面中。
在設計界面左側document outline窗口面板中選中View Controller,打開標識檢測器,在class屬性中選擇ViewController,注意在屬性檢測器中勾選 Is Initial View Controller
,設置為默認視圖控制器。再選中Table View,打開屬性檢測器窗口,在content屬性下拉菜單中有兩個選項,Dynamic Prototypes和Static Cells,即動態(tài)表和靜態(tài)表,這里我們選擇動態(tài)表。再選中Table View Cell,打開屬性檢測器,在style屬性中系統(tǒng)提供了幾種單元格樣式,默認是custom樣式,Identifier屬性表示可重用單元格的標識符,類似于一個ID,可自由定義,這里我們設為CellIdentifier,然后在代碼中獲取單元格對象的時候就不需要再實例化單元格了,直接引用即可。
比如,之前我們獲取單元格對象時的代碼是這樣的:
NSString *identifierString = @"simpleTableViewCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifierString];
if (cell==nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifierString];
}
在故事板文件中設置了IDentifier屬性之后,代碼如下:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellIentifier"];
將 ViewController.h
文件中繼承的父類ViewController替換成UITableViewController,并定義一個數(shù)據(jù)屬性:
#import <UIKit/UIKit.h>
@interface ViewController : UITableViewController
@property (strong, nonatomic)NSArray *simpleData;
@end
數(shù)組內容從文件Property List文件中讀取。在工程文件存放代碼的目錄中新建一個plist文件,添加測試數(shù)據(jù),如圖所示:
在ViewController.m文件中讀取plist中的數(shù)據(jù),同時實現(xiàn)數(shù)據(jù)源協(xié)議的兩個方法:tableView: numberOfRowsInSection:
和 tableView: cellForRowAtIndexPath:
,分別用于設置節(jié)中的單元格行數(shù)和單元格視圖,代碼如下:
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"simpleData" ofType:@"plist"];
self.simpleData = [[NSArray alloc] initWithContentsOfFile:filePath];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [self.simpleData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *identifierString = @"simpleTableViewCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifierString];
if (cell==nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifierString];
}
NSInteger rowIndex = [indexPath row];
NSDictionary *cellDictionary = [self.simpleData objectAtIndex:rowIndex];
cell.textLabel.text = [cellDictionary objectForKey:@"name"];
cell.imageView.image = [UIImage imageNamed:[cellDictionary objectForKey:@"cover"]];
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
return cell;
}
@end
運行效果如下:
注意:在iOS7之后,狀態(tài)欄是透明的,所以上圖中的應用界面頂部和狀態(tài)欄重疊在一起,不過不用擔心,通常我們的應用頂部會有導航欄或者直接隱藏狀態(tài)欄,甚至可以設置應用界面距離頂部的距離,避免上圖重疊引起的視覺問題。
這樣,通過上述介紹就能實現(xiàn)iOS應用中簡單的表視圖效果,較為基礎,在下一篇文章中我們一起學習如何自定義視圖實現(xiàn)更為豐富的單元格樣式,并為表視圖添加搜索欄,歡迎關注。