1. 首先,Controller須要實現兩個 delegate ,分別是 UITableViewDelegate 和 UITableViewDataSource app
2.而後 UITableView對象的 delegate要設置爲 self。 函數
3. 而後就能夠實現這些delegate的一些方法拉。 spa
(1)- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView; .net
這個方法返回 tableview 有多少個section 對象
(2)- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section; blog
這個方法返回 對應的section有多少個元素,也就是多少行。 ip
(3)- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath; ci
這個方法返回指定的 row 的高度。 get
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section; string
這個方法返回指定的 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 類型。其中有一個主標題 cell.textLabel 還有一個副標題cell.detailTextLabel, 還有一個 image在最前頭 叫
cell.imageView. 還能夠設置右邊的圖標,經過cell.accessoryType 能夠設置是飽滿的向右的藍色箭頭,仍是單薄的向右箭頭,
仍是勾勾標記。
(5)- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
返回指定的 section 的header的高度
(6)- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
返回指定的section 的 header 的 title,若是這個section header 有返回view,那麼title就不起做用了。
(7) - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
返回指定的 section header 的view,若是沒有,這個函數能夠不返回view
(8) - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
當用戶選中某個行的cell的時候,回調用這個。可是首先,必須設置tableview的一個屬性爲能夠select 才行。
若是不但願響應select,那麼就能夠用下面的代碼設置屬性:
下面是響應select 點擊函數,根據哪一個section,哪一個row 本身作出響應就好啦。
如何讓cell 可以響應 select,可是選中後的顏色又不發生改變呢,那麼就設置
cell.selectionStyle = UITableViewCellSelectionStyleNone;
(9)如何設置tableview 每行之間的 分割線
若是不須要分割線,那麼就設置屬性爲 UITableViewCellSeparatorStyleNone 便可。
(10)如何設置 tableview cell的背景顏色
(11) - (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
這個函數響應,用戶點擊cell 右邊的 箭頭(若是有的話)
(12)如何設置tableview 能夠被編輯
首先要進入編輯模式:
若是要退出編輯模式,確定就是設置爲NO
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
返回當前cell 要執行的是哪一種編輯,下面的代碼是 返回 刪除 模式
-(void) tableView:(UITableView *)aTableView
commitEditingStyle:(UITableViewCellEditingStyle) editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
通知告訴用戶編輯了 哪一個cell,對應上面的代碼,咱們在這個函數裏面執行刪除cell的操做。