您的位置:首页 > 移动开发 > IOS开发

Iphone开发(十一)从plist文件读取列表数据并添加索引

2012-12-19 11:03 459 查看
我们知道在IOS开发中,系统级的还是我们自己的一些配置文件一般是用plist文件来保存的,有的时候我们的数据不需要在代码中创建,而是以plist格式保存,这时我们就需要在代码中将其取出,当然前提肯定是文件在项目资源里。上次我们实现了简单的列表,今天我们在上次的基础上扩展一下,上次我们列表的数据源是在viewDidLoad中自己随手构建的,今天呢我们将保存在一个plist文件中的量比较多的数据添加到列表中。

首先我们要有一个充满了数据的plist文件,我们如果自己new的话按下图:



现在我已经构造好了,看截图:



命名为dota.plist,内容是一个dictionary键值对,键有六个,分别是什么什么型英雄,如上所示,分别对应了六个数组,数组里保存的是多个字符串。好了,现在我们要在viewDidLoad方法里将内容读取出来。所以我们要在声明时先声明一个NSDictionary类型用来保存该文件,再声明一个NSArray类型来保存六个Key。

viewController.h:

[plain] view
plaincopy

<span style="font-size:18px;">#import <UIKit/UIKit.h>  

  

@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>  

@property (nonatomic ,strong)NSDictionary *myDotaDictionary;  

@property (nonatomic ,strong)NSArray *myKeys;  

  

@end</span>  

然后在viewDidLoad方法中将文件读取出来:

[plain] view
plaincopy

<span style="font-size:18px;">-(void)viewDidLoad  

{  

    [super viewDidLoad];  

    NSString *path = [[NSBundle mainBundle] pathForResource:@"dota" ofType:@"plist"];//找到该plist的路径  

   // NSLog(@"%@",path);  

    myDotaDictionary = [[NSDictionary alloc]initWithContentsOfFile:path];  

      

     

    self.myKeys=[[NSArray alloc]initWithArray:[myDotaDictionary allKeys]];  

      

      

}</span>  

这时就会显示一个列表了,列表内容是我们自制的那个plist文件内容。我们可以在xib文件中设为分组或者设为普通格式,不管是什么模式,我们都可以添加索引用来方便的定位列表,添加索引的方法是:

[plain] view
plaincopy

<span style="font-size:18px;">-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView</span>  

该方法返回一个数组,这个数组中的每一个元素依次按顺序对应列表中的每一个section,所以如果要显示索引的话,我们需要定义该方法并给出一个返回值,我们暂且返回我们的keys数组吧,这样每个索引名就和section标题是一样的了。注意返回的数组要和分区数一样,来实现索引和分区一一对应。下面是整个代码:

viewController.m:

[plain] view
plaincopy

<span style="font-size:18px;">#import "ViewController.h"  

  

  

@implementation ViewController  

@synthesize myDotaDictionary;  

@synthesize myKeys;  

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView  

{     

    //该方法返回有多个少分区,也就是多少组(如果分组模式的话)  

    return [myKeys count];  

}  

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section  

{  

    //该方法返回每一个分区有多少行。该方法会遍历很多次,每次的参数section都不同  

    NSString *tmp =[myKeys objectAtIndex:section];//根据section取出该组的标题  

    //也就是键值对中的key,下一步用这个key取出对应的数组,也就是该区内容。  

    NSArray *tmpArray=[myDotaDictionary objectForKey:tmp];  

    return [tmpArray count];  

      

          

      

}  

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

{  

    //返回一个字符串,用作给定分区的标题  

    return [myKeys objectAtIndex:section];  

}  

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  

{  

    //这个就不是数据源中的方法了。  

    //该方法返回指定分区的每一行  

    NSUInteger sectionNum = [indexPath section];  

    NSUInteger row = [indexPath row];  

      

    NSString *title = [myKeys objectAtIndex:sectionNum];  

    NSArray *contents = [myDotaDictionary objectForKey:title];  

      

    static NSString *identifier = @"sss";  

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];  

    if (cell==nil) {  

        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];  

    }  

    cell.textLabel.text= [contents objectAtIndex:row];  

      

    return cell;  

}  

  

  

-(void)viewDidLoad  

{  

    [super viewDidLoad];  

    NSString *path = [[NSBundle mainBundle] pathForResource:@"dota" ofType:@"plist"];//找到该plist的路径  

   // NSLog(@"%@",path);  

    myDotaDictionary = [[NSDictionary alloc]initWithContentsOfFile:path];  

      

     

    self.myKeys=[[NSArray alloc]initWithArray:[myDotaDictionary allKeys]];  

    //上一步是将字典中的key取出来作为每一组的标题用,取出来后来了一个排序。  

  

      

}  

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

{  

    //该方法响应列表中行的点击事件  

     

    NSArray *arrayInSection=[myDotaDictionary objectForKey: [myKeys objectAtIndex:indexPath.section]];  

    NSString *heroSelected=[arrayInSection objectAtIndex:indexPath.row];  

      

    UIAlertView *myAlertView;  

    myAlertView = [[UIAlertView alloc]initWithTitle:@"dota群英传" message:heroSelected delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil];  

    [myAlertView show];  

}  

-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView  

{  

    return myKeys;  

    //Keys中的每一个值对应一个分区,也可以自定义。  

    //    NSArray *tmpIndexArray = [[NSArray alloc]initWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",nil];  

    //    return tmpIndexArray;  

}  

- (void)viewDidUnload  

{  

    [super viewDidUnload];  

    // Release any retained subviews of the main view.  

     

}  

  

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation  

{  

    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);  

}  

  

@end  

</span>  

我们可以在xib文件中设置列表的格式为分组或者正常格式来显示不同的效果:





转自holydancer的CSDN专栏,原文地址:http://blog.csdn.net/holydancer/article/details/7433430
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  IOS基础 IPHONE XCode
相关文章推荐