UITableView 使用方法(一) - 创建简单的列表

来源:互联网 发布:淘宝自动回复问题大全 编辑:程序博客网 时间:2024/06/06 01:52

UITableView是iPhone中比较常用的,用的比较多的控件,下面我们使用UITableView创建一个简单的表格,效果如下:


如果要表格中增加数据的话,需要增加UITableViewDataSource协议。

如果需要响应用户单击的话,需要增加UITableViewDelegate协议。


1、创建项目:使用模板Single View Application新建一个项目,仅支持iPhone。

2、在ViewController.h中增加UITableViewDataSource和UITableViewDelegate协议,如下:

[cpp] view plaincopy
  1. #import <UIKit/UIKit.h>  
  2.   
  3. @interface ViewController : UIViewController<UITableViewDataSource, UITableViewDelegate> {  
  4.   
  5.     NSArray * listData;  
  6. }  
  7.   
  8. @property ( nonatomic, retain) NSArray *listData;  
  9.   
  10. @end  

3、往列表中增加数据,实现UITableViewDataSource协议,如下:

[cpp] view plaincopy
  1. //返回总行数  
  2. -(NSInteger ) tableView:(UITableView *)tableView  
  3.   
  4.   numberOfRowsInSection:(NSInteger )section  
  5.   
  6. {      
  7.     return [ self.listData count ];  
  8.       
  9. }  
  10.   
  11. // 添加每一行的信息  
  12. - (UITableViewCell *) tableView:(UITableView *)tableView  
  13.           cellForRowAtIndexPath:(NSIndexPath *)indexPath  
  14.   
  15. {     
  16.       
  17.     NSString *tag=@"tag";  
  18.       
  19.     UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:tag];  
  20.       
  21.     if (cell==nil ) {  
  22.         cell=[[[ UITableViewCell alloc ] initWithFrame : CGRectZero  
  23.                                         reuseIdentifier:tag] autorelease];  
  24.     }      
  25.       
  26.     NSUInteger row=[indexPath row];  
  27.       
  28.     //设置文本  
  29.     cell.text =[listData objectAtIndex :row];  
  30.       
  31.     //选中后的颜色又不发生改变,进行下面的设置  
  32.     //cell.selectionStyle = UITableViewCellSelectionStyleNone;   
  33.       
  34.     //不需要分割线  
  35.     //tableView.separatorStyle=UITableViewCellSeparatorStyleNone;    
  36.       
  37.     return cell;  
  38.       
  39. }  

4、响应用户单击事件,实现UITableViewDelegate协议,如下:

[cpp] view plaincopy
  1. //响应用户单击事件  
  2. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{   
  3.       
  4.     UIAlertView* showSelection;  
  5.     NSString* message;  
  6.       
  7.     message = [[NSString alloc]initWithFormat:@"You chose the : %@",  
  8.                      [self.listData objectAtIndex:indexPath.row]];  
  9.       
  10.     showSelection = [[UIAlertView alloc]  
  11.                      initWithTitle:@"Selected"  
  12.                      message:message  
  13.                      delegate:nil  
  14.                      cancelButtonTitle:@"OK"  
  15.                      otherButtonTitles:nil];     
  16.       
  17.     [showSelection autorelease];  
  18.     [showSelection show];  
  19. }  

5、往ViewController中增加UITableView,并将UITableView的delegate和dataSource连接到ViewController。如下图所示:

































6、完整的代码如下:

[cpp] view plaincopy
  1. #import "ViewController.h"  
  2.   
  3. @interface ViewController ()  
  4.   
  5. @end  
  6.   
  7. @implementation ViewController  
  8.   
  9. @synthesize listData;  
  10.   
  11. - (void)viewDidLoad  
  12. {  
  13.       
  14.     self.listData =[[NSArray alloc] initWithObjects:@"Item1",@"Item2",@"Item3", @"Item4", @"Item5", @"Item6", @"Item7",nil];;  
  15.   
  16.     [super viewDidLoad];  
  17.     // Do any additional setup after loading the view, typically from a nib.  
  18. }  
  19.   
  20. - (void)viewDidUnload  
  21. {  
  22.     self.listData = nil;  
  23.       
  24.     [super viewDidUnload];  
  25.     // Release any retained subviews of the main view.  
  26. }  
  27.   
  28. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation  
  29. {  
  30.     return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);  
  31. }  
  32.   
  33.   
  34. #pragma mark - Table view data source delegate  
  35.   
  36. //返回总行数  
  37. -(NSInteger ) tableView:(UITableView *)tableView  
  38.   numberOfRowsInSection:(NSInteger )section  
  39. {      
  40.     return [ self.listData count ];  
  41. }  
  42.   
  43. // 添加每一行的信息  
  44. - (UITableViewCell *) tableView:(UITableView *)tableView  
  45.           cellForRowAtIndexPath:(NSIndexPath *)indexPath  
  46.   
  47. {     
  48.       
  49.     NSString *tag=@"tag";  
  50.       
  51.     UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:tag];  
  52.       
  53.     if (cell==nil ) {  
  54.         cell=[[[ UITableViewCell alloc ] initWithFrame : CGRectZero  
  55.                                         reuseIdentifier:tag] autorelease];  
  56.     }      
  57.       
  58.     NSUInteger row=[indexPath row];  
  59.       
  60.     //设置文本  
  61.     cell.text =[listData objectAtIndex :row];  
  62.       
  63.     //选中后的颜色又不发生改变,进行下面的设置  
  64.     //cell.selectionStyle = UITableViewCellSelectionStyleNone;   
  65.       
  66.     //不需要分割线  
  67.     //tableView.separatorStyle=UITableViewCellSeparatorStyleNone;    
  68.       
  69.     return cell;  
  70.       
  71. }  
  72.   
  73. #pragma mark - Table view data delegate  
  74.   
  75. //响应用户单击事件  
  76. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{   
  77.       
  78.     UIAlertView* showSelection;  
  79.     NSString* message;  
  80.       
  81.     message = [[NSString alloc]initWithFormat:@"You chose the : %@",  
  82.                      [self.listData objectAtIndex:indexPath.row]];  
  83.       
  84.     showSelection = [[UIAlertView alloc]  
  85.                      initWithTitle:@"Selected"  
  86.                      message:message  
  87.                      delegate:nil  
  88.                      cancelButtonTitle:@"OK"  
  89.                      otherButtonTitles:nil];     
  90.       
  91.     [showSelection autorelease];  
  92.     [showSelection show];  
  93. }  
  94.   
  95. @end  

附上实例源代码下载:

http://download.csdn.net/detail/ztp800201/4500391

编译环境:Mac OS 10.7.4 + XCode4.3.3 + iOS5.1

0 0
原创粉丝点击