iOS開發(fā)UITableView基本使用方法總結(jié)
UITableView基本使用方法
1.首先,Controller需要實現(xiàn)兩個delegate ,分別是UITableViewDelegate 和UITableViewDataSource
2.然后 UITableView對象的 delegate要設(shè)置為 self。
3.然后就可以實現(xiàn)這些delegate的一些方法拉。
(1)- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
這個方法返回 tableview 有多少個section
- //返回有多少個Sections
 - - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
 - {
 - return 1;
 - }
 
(2)- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section;
這個方法返回 對應(yīng)的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 的高度。
- - (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就不起作用了。
- - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
 - {
 - if (tableView == tableView_)
 - {
 - if (section == 0)
 - {
 - return @"title 1";
 - }
 - else if (section == 1)
 - {
 - return @"title 2";
 - }
 - else
 - {
 - return nil;
 - }
 - }
 - else
 - {
 - return nil;
 - }
 - }
 
(7) - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
返回指定的 section header 的view,如果沒有,這個函數(shù)可以不返回view
- - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
 - {
 - if (section == 0)
 - {
 - UIView* header = [[[NSBundle mainBundle] loadNibNamed: @"SettingHeaderView"
 - owner: self
 - options: nil] lastObject];
 - else
 - {
 - return nil;
 - }
 - }
 
(8) - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
當用戶選中某個行的cell的時候,回調(diào)用這個。但是首先,必須設(shè)置tableview的一個屬性為可以select 才行。
- TableView.allowsSelection=YES;
 
- cell.selectionStyle=UITableViewCellSelectionStyleBlue;
 
如果不希望響應(yīng)select,那么就可以用下面的代碼設(shè)置屬性:
- TableView.allowsSelection=NO;
 
下面是響應(yīng)select 點擊函數(shù),根據(jù)哪個section,哪個row 自己做出響應(yīng)就好啦。
- - (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 能夠響應(yīng) select,但是選中后的顏色又不發(fā)生改變呢,那么就設(shè)置
cell.selectionStyle = UITableViewCellSelectionStyleNone;
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 - {
 - //cell被選中后的顏色不變
 - cell.selectionStyle = UITableViewCellSelectionStyleNone;
 - }
 
(9)如何設(shè)置tableview 每行之間的分割線
- self.tableView.separatorStyle=UITableViewCellSeparatorStyleSingleLine;
 
如果不需要分割線,那么就設(shè)置屬性為 UITableViewCellSeparatorStyleNone 即可。
(10)如何設(shè)置 tableview cell的背景顏色
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 - {
 - //設(shè)置背景顏色
 - cell.contentView.backgroundColor=[UIColor colorWithRed:0.957 green:0.957 blue:0.957 alpha:1];
 - }
 
(11) - (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
這個函數(shù)響應(yīng),用戶點擊cell 右邊的 箭頭(如果有的話)
(12)如何設(shè)置tableview 可以被編輯
首先要進入編輯模式:
- [TableView setEditing:YES animated:YES];
 
如果要退出編輯模式,肯定就是設(shè)置為NO
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
返回當前cell 要執(zhí)行的是哪種編輯,下面的代碼是返回刪除模式
- - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
 - {
 - return UITableViewCellEditingStyleDelete;
 - }
 
-(void) tableView:(UITableView *)aTableView
commitEditingStyle:(UITableViewCellEditingStyle) editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
通知告訴用戶編輯了 哪個cell,對應(yīng)上面的代碼,我們在這個函數(shù)里面執(zhí)行刪除cell的操作。
- -(void) tableView:(UITableView *)aTableView
 - commitEditingStyle:(UITableViewCellEditingStyle) editingStyle
 - forRowAtIndexPath:(NSIndexPath *)indexPath
 - {
 - [chatArray removeObjectAtIndex:indexPath.row];
 - [chatTableView reloadData];
 - }
 
(13)如何獲得 某一行的CELL對象
- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath;















 
 
 


 
 
 
 