標籤:uitableview uiviewcontroller delegate ios7 表視圖
列表視圖,顧名思義就是將資料的內容用列表的形式顯示在螢幕上的視圖。在ios中列表視圖以UITableView實現,這個類在實際應用中非常的頻繁,但是對於初學者來說不是非常容易理解。這裡將UITableView的主要用法總結一下以備查。
UITableView定義在標頭檔UITableView.h中,具體的定義可以查看官方文檔;從定義中可以看出,UITableView繼承自UIScrollView類,因此在支援方便地顯示列表資料的同時,還天生支援垂直滾動操作。組成列表的每一個元素稱為UITableViewCell執行個體。一個UITableViewCell也是應用非常廣泛的類,定義可見官方文檔。在具體的使用過程中,可以建立一個獨立的UITableView,也可以直接建立一個UITableViewController。這裡主要記錄建立UITableView的方法,下篇記錄通過列表視圖控制器使用UITableView。
UITableView類中定義了style屬性:
@property(nonatomic, readonly) UITableViewStyle style
每一個UITableView都可以選擇兩種style之一,即分組模式和平面模式,這兩種模式定義在枚舉變數UITableViewStyle中:
typedef enum { UITableViewStylePlain, UITableViewStyleGrouped} UITableViewStyle;
每一個列表視圖的組成都是相似的,都是由一個表頭視圖+表體+表尾視圖構成。其中表頭和表尾兩個視圖預設為nil,需要時可以建立自訂視圖添加到表頭和表尾。定義如下:
@property(nonatomic, retain) UIView *tableHeaderView;@property(nonatomic, retain) UIView *tableFooterView;
除表頭和表尾之外,表體則由一串UITableViewCell(下面簡稱cell)構成。如果是分組表視圖,則多個UITableViewCell構成一個section,每個section也有頭和尾視圖。
下面簡單建立一個demo展示一下如何建立一個UITableView。這裡假定大家都瞭解xcode的基本操作,所以就不再一步一步地了,簡單敘述即可。不懂得可以去百度一下“xcode建立工程”。
建立一個single view application,在新產生的ViewController.m檔案中重寫loadView方法,建立一個UITableView視圖。(別忘了把alloc的視圖在dealloc函數中釋放。)
- (void)loadView{ CGFloat width = [UIScreen mainScreen].bounds.size.width; CGFloat height = [UIScreen mainScreen].bounds.size.height; UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width,height)]; self.view = backgroundView; _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, width, height) style:UITableViewStylePlain]; [self.view addSubview:_tableView]; [_tableView release];}
編譯運行,顯示如:
表視圖的協議方法——這是非常重要的部分,因為我們建立一個表視圖,目的就是讓視圖可以顯示資料,否則一個空空的表視圖與廢物無二。表視圖所定義的協議方法由代理方法delegate和資料來源方法data source方法組成。委託方法一般用於實現個人化處理表視圖的基本樣式(如單元格的高度等)以及捕捉單元格選中的響應;資料來源方法用於完成表中的資料,如指定單元格數,以及建立每一個單元格。
要實現代理和資料來源方法,首先需要讓當前視圖控制器支援UITableViewDelegate和UITableViewDataSource協議。做如下修改:
@interface ViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>
並且在tableView建立完成後,將tableView的delegate和dataSource設定為self,即委託給當前視圖控制器來控製表視圖的資料顯示和響應。
_tableView.delegate = self;_tableView.dataSource = self;
delegate和data source協議有兩個方法是必須實現的:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
這兩個方法分別用於產生每一個cell,以及指定當前section共有多少行。實現這兩個方法是想要在表視圖中顯示資料必須實現的最低要求。
我們在視圖控制器標頭檔中聲明一個NSArray *model(retain屬性),並在viewDidLoad中將[UIFont familyNames]賦給這個屬性。
在視圖控制器中實現這兩個代理方法:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return [_model count];}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *identify = @"TableViewCell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identify]; if (nil == cell) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identify]; cell.textLabel.text = self.model[indexPath.row]; } return cell;}在cellForRowAtIndexPath方法中,首先會檢查是否有閑置的單元格,如果沒有閑置的單元格,則會建立一個cell並將其返回。參數indexPath表示目前正在建立的單元格位於整個表視圖的第幾行。
編譯,運行,顯示結果:
如果希望實現對選中某個單元格的響應,只需要實現下面代理方法即可。在代理方法中可以實現建立新的視圖控制器並控制其載入到螢幕上。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;