任何事情之前,我的plist之前我添加项目1和项目2- [__ NSCFArray objectAtIndex:]:指数(1)超出范围(1)
然后作为项目工作的精细与下面的代码进步,我必须在我的plist中添加几个项目。然后发生错误。
的问题是,我有了项0,第1项,第2项为字典如所述下方的“根”的plist:顺便
的的Plist具有除了在串相同的数据省。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
<dict>
<key>Province</key>
<string>Metro Manila</string>
<key>Cities</key>
<array>
<dict>
<key>Places</key>
<string>Chowking</string>
</dict>
</array>
</dict>
<dict>
<key>Province</key>
<string>Pampanga</string>
<key>Cities</key>
<array>
<dict>
<key>Places</key>
<string>Jollibee</string>
</dict>
<dict>
<key>Places</key>
<string>McDonald's</string>
</dict>
<dict>
<key>Places</key>
<string>Pizza Hut</string>
</dict>
</array>
</dict>
<dict>
<key>Province</key>
<string>Pangasinan</string>
<key>Cities</key>
<array>
<dict>
<key>Places</key>
<string>Jollibee</string>
</dict>
<dict>
<key>Places</key>
<string>McDonald's</string>
</dict>
</array>
</dict>
</array>
</plist>
这是我Controller.m或者的代码
//
// RootViewController.m
// TableView
//
// Created by OSX on 10/10/12.
// Copyright (c) 2012 OSX. All rights reserved.
//
#import "RootViewController.h"
#import "CityViewController.h"
@interface RootViewController()
@end
@implementation RootViewController {
NSArray * luzonRegion;
NSArray * visayasRegion;
NSArray * mindanaoRegion;
}
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = @"Region";
[self loadRegionPlist];
}
- (void)loadRegionPlist
{
NSString *plistLuzon = [[NSBundle mainBundle] pathForResource: @"luzonPlist" ofType: @"plist"];
luzonRegion = [[NSArray alloc] initWithContentsOfFile: plistLuzon];
NSString *plistVisayas = [[NSBundle mainBundle] pathForResource: @"visayasPlist" ofType: @"plist"];
visayasRegion = [[NSArray alloc] initWithContentsOfFile: plistVisayas];
NSString *plistMindanao = [[NSBundle mainBundle] pathForResource: @"mindanaoPlist" ofType: @"plist"];
mindanaoRegion = [[NSArray alloc] initWithContentsOfFile: plistMindanao];
NSLog(@"%@", luzonRegion);
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
switch (section) {
case 0:
return [luzonRegion count];
break;
case 1:
return [visayasRegion count];
break;
case 2:
return [mindanaoRegion count];
break;
default:
break;
}
return section;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
// Configure the cell...
NSDictionary *luzonDictionary = [luzonRegion objectAtIndex:indexPath.row];
NSDictionary *visayasDictionary = [visayasRegion objectAtIndex:indexPath.row];
NSDictionary *mindanaoDictionary = [mindanaoRegion objectAtIndex:indexPath.row];
switch (indexPath.section) {
case 0:
cell.textLabel.text = [luzonDictionary objectForKey: @"Province"];
break;
case 1:
cell.textLabel.text = [visayasDictionary objectForKey: @"Province"];
break;
case 2:
cell.textLabel.text = [mindanaoDictionary objectForKey: @"Province"];
break;
default:
break;
}
[cell setAccessoryType: UITableViewCellAccessoryDisclosureIndicator];
return cell;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
switch (section) {
case 0:
return @"Luzon";
break;
case 1:
return @"Visayas";
break;
case 2:
return @"Mindanao";
break;
default:
break;
}
return nil;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
CityViewController *cityViewController = [[CityViewController alloc] initWithStyle:UITableViewStylePlain];
NSDictionary *luzonDictionary = [luzonRegion objectAtIndex:indexPath.row];
NSDictionary *visayasDictionary = [visayasRegion objectAtIndex:indexPath.row];
NSDictionary *mindanaoDictionary = [mindanaoRegion objectAtIndex:indexPath.row];
switch (indexPath.section) {
case 0:
cityViewController.title = [luzonDictionary objectForKey: @"Province"];
cityViewController.Cities = [luzonDictionary objectForKey: @"Cities"];
break;
case 1:
cityViewController.title = [visayasDictionary objectForKey: @"Province"];
cityViewController.Cities = [visayasDictionary objectForKey: @"Cities"];
break;
case 2:
cityViewController.title = [mindanaoDictionary objectForKey: @"Province"];
cityViewController.Cities = [mindanaoDictionary objectForKey: @"Cities"];
break;
default:
break;
}
[self.navigationController pushViewController:cityViewController animated:YES];
}
@end
的NSLog和调试地区输出:
2012-12-17 11:24:21.023 TableViewPlist[24315:c07] (
{
Cities = (
{
Places = Chowking;
}
);
Province = "Metro Manila";
},
{
Cities = (
{
Places = Jollibee;
},
{
Places = "McDonald's";
},
{
Places = "Pizza Hut";
}
);
Province = Pampanga;
},
{
Cities = (
{
Places = Jollibee;
},
{
Places = "McDonald's";
}
);
Province = Pangasinan;
}
)
2012-12-17 11:24:21.043 TableViewPlist[24315:c07] *** Terminating app due to uncaught exception 'NSRangeException', reason: '-[__NSCFArray objectAtIndex:]: index (1) beyond bounds (1)'
*** First throw call stack:
(0x1c94012 0x10d1e7e 0x1c93deb 0x1c887e0 0x36aa 0xd3f4b 0xd401f 0xbc80b 0xcd19b 0x6992d 0x10e56b0 0x2290fc0 0x228533c 0x2290eaf 0x1088cd 0x511a6 0x4fcbf 0x4fbd9 0x4ee34 0x4ec6e 0x4fa29 0x52922 0xfcfec 0x49bc4 0x49dbf 0x49f55 0xc472d84 0x52f67 0x2cb2 0x167b7 0x16da7 0x17fab 0x29315 0x2a24b 0x1bcf8 0x1befdf9 0x1befad0 0x1c09bf5 0x1c09962 0x1c3abb6 0x1c39f44 0x1c39e1b 0x177da 0x1965c 0x293d 0x2865 0x1)
libc++abi.dylib: terminate called throwing an exception
(lldb)
而不是显示plist中表现出一定的实际阵列,luzonRegion的 - 这将是更加有用。另外,什么行会抛出错误? – rdelmar
我已经添加了luzonRegion的实际数组。该项目有没有问题,而是,这一个:http://f.cl.ly/items/233S423H081o290u3W0L/Screen%20Shot%202012-12-17%20at%2011.15.51%20AM.png – Jahm
我的意思是显示结果的NSLog(@“%@”,luzonRegion),而不是pList。 – rdelmar