1. 程式人生 > >UITableView總結:基本使用方法介紹

UITableView總結:基本使用方法介紹

最近在做iphone GRE背單詞過程中涉及到很多UITableView的操作,藉此機會好好梳理總結一下。本節主要介紹UITableView的基本使用方法。

一)UITableView所在的UIViewController宣告兩個delegate:UITableViewDelegate和UITableViewDataSource。

二)將UITableView物件的delegate設定成self。

三)根據實際需要實現delegate的具體方法,這裡簡要介紹一下常用的方法和屬性。

(1)- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 該方法返回tableview有多少個section。

(2)- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section 該方法返回對應的section有多少個元素,也就是每個section對應有多少個cell。

(3)- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 該方法返回指定的row高度。

- (CGFloat)tableView:(UITableView *)tableView  heightForHeaderInSection:(NSInteger)section 該方法返回指定的section的header view的高度。

- (CGFloat)tableView:(UITableView *)tableView  heightForFooterInSection:(NSInteger)section 該方法返回指定的section的footer view的高度。

(4)- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 該方法返回指定row的cell,在此函式中使用者可以根據自己的需求定義cell的屬性和顯示風格等(主標題cell.textLabel,副標題cell.detailTextLabel,背景cell.imageView,圖示cell.accessoryType等等)。

(5)- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 該函式返回指定section的header的titile。

(6)- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 該函式返回指定section header的view

(7)  -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 使用者選中某cell時的回撥函式。

(8)- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath 該方法獲取某一cell物件。

(9)如果想讓cell能響應選中事件,但是選中後的顏色不發生改變的話,設定cell.selectionStyle = UITableViewCellSelectionStyleNone。

(10)如果想刪除cell之間的分割線,設定

tableview.separatorStyle = UITableViewCellSeparatorStyleNone。

以上介紹的方法及屬性僅僅是GRE用到的,另外還有很多方法和屬性未涉及。本篇文章只是拋磚引玉,等我們需要用到的時候可以參考xCode的聯機幫助手冊。

下節我給大家介紹一下UITableViewCell定製的用法。