Iphone开发(十)简单的列表tableView与行的响应事件

来源:互联网 发布:工商银行软件中心待遇 编辑:程序博客网 时间:2024/06/14 05:15

列表是移动开发中的视图中的重要部分,往往又是控件中最复杂呢,今天演示一个最简单的列表,先来对列表有个大概的了解。虽然是最简单的,但还是稍稍有些复杂.

在iphone开发中的列给称为tableView,在大的方向上分为两种,一种是普通列表plain,一组是分组列表grouped;



上面是两种不同的表格式,另外我们在上面可以发现有一个关键字Section,称为分区,在普通视图中,那个California和下面的Section Footer之前的就是一个分区,California称为Section Header,在每个分区的开头部分,Section Footer在每个分区的结尾部分,在分组视图中,每一个组就是一个section,然后每个分区中有若干行,称为row。

所以我们如果要创建一个列表的话,除了要设定格式外(分组与否),还要提供显示的内容(数据源),然后显示的样式(多少分区,每区多少行),并将这些关联起来。这些都需要在方法中设定,这些方法分别在两个协议里(是不是类似date picker之流?),一个数据源协议,一个委托协议。

好了,现在开始看代码吧,新建一个项目,打开xib文件进行如下操作:


格式设定为plain


然后在viewController中实现两个协议:

viewController.h:

[plain] view plaincopy
  1. #import <UIKit/UIKit.h>  
  2.   
  3. @interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>  
  4.   
  5. @end  
然后先实现数据源协议中的三个方法来规划这个列表:

numberofSectionInTableView: //这个列表有多少个区,我们只来一个区先

tableView:tableViewnumberOfRowInSection://每个区都有多少行

tableView:titleForHeaderInSection://返回每个分区的标题

我们用这三个方法就可以了,然后再实现委托方法中的两个方法来画出列表和响应点击:

tableView:cellForRowAtIndexPath://这个方法返回每一行的内容,有多少行这个方法就会运行多少次。

tableView:didSelectRowAtIndexPath://这个方法响应点击哪一行。

另外,需要注意的是,列表中显示的内容一般都是在viewDidLoad中加载。

下面看实现部分代码:注意我又加了一个数组类型的属性myListArray;


viewController.m:

[plain] view plaincopy
  1. #import "ViewController.h"  
  2.   
  3.   
  4.   
  5. @implementation ViewController  
  6. @synthesize myListArray;  
  7. -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView  
  8. {  
  9.     return 1;  
  10. }  
  11. -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section  
  12. {  
  13.     return [myListArray count];  
  14.     //只有一组,数组数即为行数。  
  15. }  
  16. -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section  
  17. {  
  18.     //我们设分区标题,不设分区标尾  
  19.     return @"dota部分英雄友情演出";  
  20. }  
  21. -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
  22. {  
  23.     //这个方法返回的cell代表每一行显示的内容,每显示一行都会运行一次此方法。  
  24.   
  25.     static NSString *NOTIFY = @"随便";  
  26.     UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:NOTIFY];  
  27.     //寻找已经加上自定义标注的当前可重用的cell。  
  28.     if (cell==nil) {  
  29.         cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NOTIFY];  
  30.         //找不到可重用的cell,就再生成一个,格式为默认,加上自定义标注;  
  31.         //if条件里面的是肯定先运行的,在第一次运行时都不可能找到可重用的加上标注的cell  
  32.         //只有列表滑动将已有行给挤出当前view时,挤出的那个cell才会变为可重用的  
  33.     }  
  34.     cell.textLabel.text=[myListArray objectAtIndex:indexPath.row];  
  35.     //将当前行的显示的label设为已定义数组中的一个。  
  36.     //indexPath.row代表当前行  
  37.     return cell;  
  38. }  
  39. - (void)viewDidLoad  
  40. {  
  41.     [super viewDidLoad];  
  42.     myListArray= [[NSArray alloc]initWithObjects:@"老牛",@"敌法",@"小Y",@"NEC",@"小小",@"白虎", nil];  
  43.       
  44. }  
  45.   
  46. - (void)viewDidUnload  
  47. {  
  48.     [super viewDidUnload];  
  49.     // Release any retained subviews of the main view.  
  50. }  
  51.   
  52. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation  
  53. {  
  54.     return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);  
  55. }  
  56.   
  57. @end  

这样就实现了一个简单的列表,看下效果:


如果要实现列表的点击响应的话,需要实现委托协议中的一个方法:

[plain] view plaincopy
  1. -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath  

该方法能够捕捉你点击的行的信息,在点击事件发生时调用,我们现在完成这个方法,达到一个点击后弹出对话框的效果,在viewController.m中实现这个方法:

[plain] view plaincopy
  1. -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath  
  2. {  
  3.     //该方法响应列表中行的点击事件  
  4.       
  5.       
  6.     NSString *heroSelected=[myListArray objectAtIndex:indexPath.row];  
  7.     //indexPath.row得到选中的行号,提取出在数组中的内容。  
  8.     UIAlertView *myAlertView;  
  9.     myAlertView = [[UIAlertView alloc]initWithTitle:@"dota群英传" message:heroSelected delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil];  
  10.     [myAlertView show];  
  11.     //点击后弹出该对话框。  
  12. }  

然后在模拟器中点击行进行测试:





转自holydancer的CSDN专栏,原文地址:http://blog.csdn.net/holydancer/article/details/7429807
原创粉丝点击