郝萌主傾心貢獻,尊重作者的勞動成果,請勿轉載。
如果文章對您有所幫助,歡迎給作者捐贈,支持郝萌主,捐贈數額隨意,重在心意^_^
我要捐贈: 點擊捐贈
Cocos2d-X源碼下載:點我傳送
1.首先,Controller需要實現兩個 delegate ,分別是 UITableViewDelegate 和 UITableViewDataSource
2.然後 UITableView對象的 delegate要設置爲 self。
3. 然後就可以實現這些delegate的一些方法。
(1)- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
這個方法返回 tableview 有多少個section
//返回有多少個Sections - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; }
(2)- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section;
這個方法返回 對應的section有多少個元素,也就是多少行。
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 10; }
(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 類型。其中有一個主標題 cell.textLabel 還有一個副標題cell.detailTextLabel, 還有一個 image在最前頭 叫 cell.imageView. 還可以設置右邊的圖標,通過cell.accessoryType 可以設置是飽滿的向右的藍色箭頭,還是單薄的向右箭頭,還是勾勾標記。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString * showUserInfoCellIdentifier = @"ShowUserInfoCell"; UITableViewCell * cell = [tableView_ dequeueReusableCellWithIdentifier:showUserInfoCellIdentifier]; if (cell == nil) { // Create a cell to display an ingredient. cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:showUserInfoCellIdentifier] autorelease]; } // Configure the cell. cell.textLabel.text=@"簽名"; cell.detailTextLabel.text = [NSString stringWithCString:userInfo.user_signature.c_str() encoding:NSUTF8StringEncoding]; }
(5)- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
返回指定的 section 的header的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { if (section ==0) return 80.0f; else return 30.0f; }
(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 才行。
TableView.allowsSelection=YES; cell.selectionStyle=UITableViewCellSelectionStyleBlue;
如果不希望響應select,那麼就可以用下面的代碼設置屬性:TableView.allowsSelection=NO;
下面是響應select 點擊函數,根據哪個section,哪個row 自己做出響應就好啦。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.section == 1) { return; } else if(indexPath.section==0) { switch (indexPath.row) { //聊天 case 0: { [self onTalkToFriendBtn]; } break; default: break; } } else { return ; } }
如何讓cell 能夠響應 select,但是選中後的顏色又不發生改變呢,那麼就設置
cell.selectionStyle = UITableViewCellSelectionStyleNone;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { //cell被選中後的顏色不變 cell.selectionStyle = UITableViewCellSelectionStyleNone; }
(9)如何設置tableview 每行之間的 分割線 self.tableView.separatorStyle=UITableViewCellSeparatorStyleSingleLine;
如果不需要分割線,那麼就設置屬性爲 UITableViewCellSeparatorStyleNone 即可。
(10)如何設置 tableview cell的背景顏色
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { //設置背景顏色 cell.contentView.backgroundColor=[UIColor colorWithRed:0.957 green:0.957 blue:0.957 alpha:1]; }
(11) - (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
這個函數響應,用戶點擊cell 右邊的 箭頭(如果有的話)
(12)如何設置tableview 可以被編輯
首先要進入編輯模式: [TableView setEditing:YES animated:YES];
如果要退出編輯模式,肯定就是設置爲NO
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
返回當前cell 要執行的是哪種編輯;
-(void) tableView:(UITableView *)aTableView
commitEditingStyle:(UITableViewCellEditingStyle) editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
通知告訴用戶編輯了 哪個cell。
(13)如何獲得 某一行的CELL對象
- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPat
郝萌主傾心貢獻,尊重作者的勞動成果,請勿轉載。
如果文章對您有所幫助,歡迎給作者捐贈,支持郝萌主,捐贈數額隨意,重在心意^_^
我要捐贈: 點擊捐贈
Cocos2d-X源碼下載:點我傳送