1

ここに初心者の質問...

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はエラーなしでロードされます。私は明らかに何か間違ったことをしているが、それが何であるかを一生理解することはできない。誰かが私にとって明白なことを指摘するのを気にしますか?

4

1 に答える 1

4

テンプレートには、静的セル用に設定された UITableView が含まれています。そして、それは実際には 1 つの静的セルです。(これが、配列を1つのアイテムに長くすると、ある種の作業が発生する理由です)

しかし、ここに静的コンテンツは必要ないようです。したがって、ストーリーボードに移動し、UITableView を選択し、属性インスペクターに移動して、コンテンツ タイプを動的プロトタイプに変更するだけです。

これで、この問題を乗り越えることができます。

編集

多少関連する問題は、おそらくストーリーボードでもプロトタイプ セルを使用したいということです。これを行うには、そのプロトタイプのセル識別子を、tableView:cellForRowAtIndexPath: で使用しているセル識別子に設定するだけです。

そして、「if (cell==nil)」の部分全体を省略します。セルは nil にはなりません。

したがって、そのメソッドは次のようになります。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell"; // <-- Make sure this matches what you have in the storyboard

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    // Configure the cell.
    cell.textLabel.text = [self.items objectAtIndex:indexPath.row];

    return cell;
}

それが役立つことを願っています。

于 2011-12-07T06:28:29.070 に答える