holydancer原创
我们知道在IOS开发中,系统级的还是我们自己的一些配置文件一般是用plist文件来保存的,有的时候我们的数据不需要在代码中创建,而是以plist格式保存,这时我们就需要在代码中将其取出,当然前提肯定是文件在项目资源里。上次我们实现了简单的列表,今天我们在上次的基础上扩展一下,上次我们列表的数据源是在viewDidLoad中自己随手构建的,今天呢我们将保存在一个plist文件中的量比较多的数据添加到列表中。
首先我们要有一个充满了数据的plist文件,我们如果自己new的话按下图:
现在我已经构造好了,看截图:
命名为dota.plist,内容是一个dictionary键值对,键有六个,分别是什么什么型英雄,如上所示,分别对应了六个数组,数组里保存的是多个字符串。好了,现在我们要在viewDidLoad方法里将内容读取出来。所以我们要在声明时先声明一个NSDictionary类型用来保存该文件,再声明一个NSArray类型来保存六个Key。
viewController.h:
1. <span >#import <UIKit/UIKit.h>
2.
3. @interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
4. @property (nonatomic ,strong)NSDictionary *myDotaDictionary;
5. @property (nonatomic ,strong)NSArray *myKeys;
6.
7. @end</span>
然后在viewDidLoad方法中将文件读取出来:
1. <span >-(void)viewDidLoad
2. {
3. [super viewDidLoad];
4. NSString *path = [[NSBundle mainBundle] pathForResource:@"dota" ofType:@"plist"];//找到该plist的路径
5. // NSLog(@"%@",path);
6. myDotaDictionary = [[NSDictionary alloc]initWithContentsOfFile:path];
7.
8.
9. self.myKeys=[[NSArray alloc]initWithArray:[myDotaDictionary allKeys]];
10.
11.
12. }</span>
这时就会显示一个列表了,列表内容是我们自制的那个plist文件内容。我们可以在xib文件中设为分组或者设为普通格式,不管是什么模式,我们都可以添加索引用来方便的定位列表,添加索引的方法是:
1. <span >-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView</span>
该方法返回一个数组,这个数组中的每一个元素依次按顺序对应列表中的每一个section,所以如果要显示索引的话,我们需要定义该方法并给出一个返回值,我们暂且返回我们的keys数组吧,这样每个索引名就和section标题是一样的了。注意返回的数组要和分区数一样,来实现索引和分区一一对应。下面是
1. <span >#import "ViewController.h"
2.
3.
4. @implementation ViewController
5. @synthesize myDotaDictionary;
6. @synthesize myKeys;
7. -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
8. {
9. //该方法返回有多个少分区,也就是多少组(如果分组模式的话)
10. return [myKeys count];
11. }
12. -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
13. {
14. //该方法返回每一个分区有多少行。该方法会遍历很多次,每次的参数section都不同
15. NSString *tmp =[myKeys objectAtIndex:section];//根据section取出该组的标题
16. //也就是键值对中的key,下一步用这个key取出对应的数组,也就是该区内容。
17. NSArray *tmpArray=[myDotaDictionary objectForKey:tmp];
18. return [tmpArray count];
19.
20.
21.
22. }
23. -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
24. {
25. //返回一个字符串,用作给定分区的标题
26. return [myKeys objectAtIndex:section];
27. }
28. -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
29. {
30. //这个就不是数据源中的方法了。
31. //该方法返回指定分区的每一行
32. NSUInteger sectionNum = [indexPath section];
33. NSUInteger row = [indexPath row];
34.
35. NSString *title = [myKeys objectAtIndex:sectionNum];
36. NSArray *contents = [myDotaDictionary objectForKey:title];
37.
38. static NSString *identifier = @"sss";
39. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
40. if (cell==nil) {
41. cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
42. }
43. cell.textLabel.text= [contents objectAtIndex:row];
44.
45. return cell;
46. }
47.
48.
49. -(void)viewDidLoad
50. {
51. [super viewDidLoad];
52. NSString *path = [[NSBundle mainBundle] pathForResource:@"dota" ofType:@"plist"];//找到该plist的路径
53. // NSLog(@"%@",path);
54. myDotaDictionary = [[NSDictionary alloc]initWithContentsOfFile:path];
55.
56.
57. self.myKeys=[[NSArray alloc]initWithArray:[myDotaDictionary allKeys]];
58. //上一步是将字典中的key取出来作为每一组的标题用,取出来后来了一个排序。
59.
60.
61. }
62. -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
63. {
64. //该方法响应列表中行的点击事件
65.
66. NSArray *arrayInSection=[myDotaDictionary objectForKey: [myKeys objectAtIndex:indexPath.section]];
67. NSString *heroSelected=[arrayInSection objectAtIndex:indexPath.row];
68.
69. UIAlertView *myAlertView;
70. myAlertView = [[UIAlertView alloc]initWithTitle:@"dota群英传" message:heroSelected delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil];
71. [myAlertView show];
72. }
73. -(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
74. {
75. return myKeys;
76. //Keys中的每一个值对应一个分区,也可以自定义。
77. // NSArray *tmpIndexArray = [[NSArray alloc]initWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",nil];
78. // return tmpIndexArray;
79. }
80. - (void)viewDidUnload
81. {
82. [super viewDidUnload];
83. // Release any retained subviews of the main view.
84.
85. }
86.
87. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
88. {
89. return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
90. }
91.
92. @end
93. </span>
我们可以在xib文件中设置列表的格式为分组或者正常格式来显示不同的效果:
关键字:IOS ,Iphone 开发 ,Iphone SDK ,tableView ,索引 ,读取plist文件