成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

iOS開發UITableView基本使用方法總結

移動開發 iOS
本文為大家呈現了iOS開發中UITableView基本使用方法總結。首先,Controller需要實現兩個delegate ,分別是UITableViewDelegate 和UITableViewDataSource;然后 UITableView對象的 delegate要設置為 self;然后就可以實現這些delegate的一些方法拉。

UITableView基本使用方法

1.首先,Controller需要實現兩個delegate ,分別是UITableViewDelegate 和UITableViewDataSource

2.然后 UITableView對象的 delegate要設置為 self。

3.然后就可以實現這些delegate的一些方法拉。

(1)- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;

這個方法返回 tableview 有多少個section

  1. //返回有多少個Sections  
  2. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView  
  3. {  
  4. return 1;  
  5. }  

(2)- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section;

這個方法返回   對應的section有多少個元素,也就是多少行。

  1. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section  
  2. {  
  3. return 10;  
  4. }  

(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 的高度。

  1. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
  2. {  
  3. static NSString * showUserInfoCellIdentifier = @"ShowUserInfoCell";  
  4. UITableViewCell * cell = [tableView_ dequeueReusableCellWithIdentifier:showUserInfoCellIdentifier];  
  5. if (cell == nil)  
  6. {  
  7. // Create a cell to display an ingredient.  
  8. cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle  
  9. reuseIdentifier:showUserInfoCellIdentifier]  
  10. autorelease];  
  11. }  
  12.  
  13. // Configure the cell.  
  14. cell.textLabel.text=@"簽名";  
  15. cell.detailTextLabel.text = [NSString stringWithCString:userInfo.user_signature.c_str() encoding:NSUTF8StringEncoding];  
  16. }  

(5)- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section

返回指定的 section 的header的高度

  1. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section  
  2. {  
  3. if (section ==0)  
  4. return 80.0f;  
  5. else  
  6. return 30.0f;  
  7. }  

(6)- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

返回指定的section 的 header  的 title,如果這個section header  有返回view,那么title就不起作用了。

  1. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section  
  2. {  
  3. if (tableView == tableView_)  
  4. {  
  5. if (section == 0)  
  6. {  
  7. return @"title 1";  
  8. }  
  9. else if (section == 1)  
  10. {  
  11. return @"title 2";  
  12. }  
  13. else  
  14. {  
  15. return nil;  
  16. }  
  17. }  
  18. else  
  19. {  
  20. return nil;  
  21. }  
  22. }  

(7) - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

返回指定的 section header 的view,如果沒有,這個函數可以不返回view

  1. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section  
  2. {  
  3. if (section == 0)  
  4. {  
  5.  
  6. UIView* header = [[[NSBundle mainBundle] loadNibNamed: @"SettingHeaderView"  
  7. owner: self  
  8. options: nil] lastObject];  
  9.  
  10. else  
  11. {  
  12. return nil;  
  13. }  
  14. }  

(8)  - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

當用戶選中某個行的cell的時候,回調用這個。但是首先,必須設置tableview的一個屬性為可以select 才行。

  1. TableView.allowsSelection=YES; 
  1. cell.selectionStyle=UITableViewCellSelectionStyleBlue;  

如果不希望響應select,那么就可以用下面的代碼設置屬性:

  1. TableView.allowsSelection=NO;  

下面是響應select 點擊函數,根據哪個section,哪個row 自己做出響應就好啦。

  1. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath  
  2. {  
  3. if (indexPath.section == 1)  
  4. {  
  5. return;  
  6. }  
  7. else if(indexPath.section==0)  
  8. {  
  9. switch (indexPath.row)  
  10. {  
  11. //聊天  
  12. case 0:  
  13. {  
  14. [self onTalkToFriendBtn];  
  15. }  
  16. break;  
  17.  
  18. default:  
  19. break;  
  20. }  
  21. }  
  22. else  
  23. {  
  24. return ;  
  25. }  
  26.  
  27. }  

如何讓cell 能夠響應 select,但是選中后的顏色又不發生改變呢,那么就設置

cell.selectionStyle = UITableViewCellSelectionStyleNone;

  1. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
  2. {  
  3. //cell被選中后的顏色不變  
  4. cell.selectionStyle = UITableViewCellSelectionStyleNone;  
  5. }  

(9)如何設置tableview  每行之間的分割線

  1. self.tableView.separatorStyle=UITableViewCellSeparatorStyleSingleLine;  

如果不需要分割線,那么就設置屬性為 UITableViewCellSeparatorStyleNone  即可。

(10)如何設置 tableview cell的背景顏色

  1. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
  2. {  
  3. //設置背景顏色  
  4. cell.contentView.backgroundColor=[UIColor colorWithRed:0.957 green:0.957 blue:0.957 alpha:1];  
  5. }  

(11) - (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath

這個函數響應,用戶點擊cell 右邊的 箭頭(如果有的話)

(12)如何設置tableview 可以被編輯

首先要進入編輯模式:

  1. [TableView setEditing:YES animated:YES];  

如果要退出編輯模式,肯定就是設置為NO

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath

返回當前cell  要執行的是哪種編輯,下面的代碼是返回刪除模式

  1. - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath  
  2. {  
  3. return UITableViewCellEditingStyleDelete;  
  4. }  

-(void) tableView:(UITableView *)aTableView

commitEditingStyle:(UITableViewCellEditingStyle) editingStyle

forRowAtIndexPath:(NSIndexPath *)indexPath

通知告訴用戶編輯了 哪個cell,對應上面的代碼,我們在這個函數里面執行刪除cell的操作。

  1. -(void) tableView:(UITableView *)aTableView  
  2. commitEditingStyle:(UITableViewCellEditingStyle) editingStyle  
  3. forRowAtIndexPath:(NSIndexPath *)indexPath  
  4. {  
  5. [chatArray removeObjectAtIndex:indexPath.row];  
  6. [chatTableView reloadData];  
  7. }  

(13)如何獲得 某一行的CELL對象

- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath;

責任編輯:閆佳明 來源: apkbus
相關推薦

2011-07-07 16:38:21

iOS UITableVie

2010-02-02 09:32:32

C++ typedef

2018-08-28 12:24:56

ButterKnife框架插件

2013-04-01 17:05:28

2011-08-23 09:44:28

LUA腳本

2009-11-16 13:57:21

PHP上傳文件

2023-02-08 08:40:21

2009-12-24 17:38:18

WPF事件觸發器

2018-06-20 10:34:56

堆棧iOSswift

2012-03-06 10:17:45

iOS SQLite3iOSSQLite3

2012-01-18 14:14:29

iOS教程iOS5

2011-02-24 13:09:10

FireFTP

2012-01-13 09:55:54

jQuery

2010-05-04 14:02:53

Oracle同義詞

2011-08-08 14:07:49

iPhone開發 字體

2009-12-24 16:36:06

WPF InkCanv

2011-08-02 17:14:41

iPhone應用 UITableVie

2013-06-20 11:21:58

iOS開發UITableView

2010-02-04 10:52:36

C++字符串分割函數

2009-06-22 13:18:00

代理Java程序
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 国产无套一区二区三区久久 | 天堂成人国产精品一区 | 免费电影av| 蜜桃免费一区二区三区 | 在线色| 欧美在线a | 国产在线精品一区二区 | 欧美一级黄视频 | 欧美大片一区 | 亚洲综合大片69999 | 一级高清视频 | 亚洲人人舔人人 | 欧美亚洲国产日韩 | 亚洲精品中文字幕 | 亚洲欧美一区二区三区国产精品 | 精品一区二区三区中文字幕 | 欧美1页 | 久久国产区 | 欧美精品一区二区三区在线播放 | 暖暖成人免费视频 | a在线观看免费 | 日韩在线一区二区 | 黄色国产| 看av网 | 亚洲视频免费观看 | www.国产.com| 毛片高清 | 午夜资源 | 亚洲高清在线 | 亚洲一区不卡在线 | 国产精品天堂 | 中文字幕人成乱码在线观看 | 久久精品视频网站 | 黄色片在线看 | 一区二区三区免费 | 狠狠涩| 亚洲成人999 | 欧美激情综合 | 亚洲毛片 | 91精品国产乱码久久久久久久 | 国产精品日本一区二区不卡视频 |