ここに初心者の質問...
ARCとストーリーボードを使用してXcode4.2でマスター/詳細アプリケーションプロジェクトを作成しました。テンプレートを次のように変更しました。
MasterViewController.h
#import <UIKit/UIKit.h>
@class DetailViewController;
@interface MasterViewController : UITableViewController
{
NSMutableArray *items;
}
@property (strong, nonatomic) DetailViewController *detailViewController;
@property (strong, nonatomic) NSMutableArray *items;
@end
MasterViewController.m(スニピット)
....
@synthesize items;
....
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.detailViewController = (DetailViewController)[[self.splitViewController.viewControllers lastObject] topViewController];
items = [[NSMutableArray alloc] initWithObjects:@"item 1", @"item 2", nil];
[self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionMiddle];
}
....
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [items count];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return @"Test 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];
}
// Configure the cell.
cell.textLabel.text = [items objectAtIndex:indexPath.row];
return cell;
}
プログラムの実行時に、このコードはこの行で失敗します。
[self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionMiddle];
この例外を除いて:
*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 1 beyond bounds [0 .. 0]'
リスト(アイテム)を1つのアイテムに切り詰めると、MasterViewControllerはエラーなしでロードされます。私は明らかに何か間違ったことをしているが、それが何であるかを一生理解することはできない。誰かが私にとって明白なことを指摘するのを気にしますか?