何よりもまず、項目 1 と項目 2 を追加する前に、私の plist は以下のコードで正常に動作しています。
その後、プロジェクトの進行に合わせて、plist にいくつかの項目を追加する必要があります。その後、エラーが発生します。
問題は、以下に示すように、ディクショナリとしてアイテム 0、アイテム 1、およびアイテム 2 を持つ「ルート」plist があることです。
ちなみに、plist には、Province の文字列を除いて同じデータが含まれています。
<?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)