任何事情之前,我的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