iOS Xcode 4.2主 - 详细信息应用程序模板抛出NSRangeException [英] iOS Xcode 4.2 Master-Detail Application Template Throwing NSRangeException

查看:149
本文介绍了iOS Xcode 4.2主 - 详细信息应用程序模板抛出NSRangeException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里有新手问题...

Newbie question here ...

我使用ARC和Storyboard在Xcode 4.2中创建了一个Master-Detail Application项目。我已将模板修改为:

I've created a Master-Detail Application project in Xcode 4.2 using ARC and Storyboard. I've modified the template to be:

#import <UIKit/UIKit.h>

@class DetailViewController;

@interface MasterViewController : UITableViewController
{
    NSMutableArray *items;
}

@property (strong, nonatomic) DetailViewController *detailViewController;
@property (strong, nonatomic) NSMutableArray *items;

@end



MasterViewController.m(剪辑)



MasterViewController.m (snipits)

....
@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;
}

程序运行时,此代码将失败:

This code will fail on this line when the program runs:

[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]'

如果我将列表(项目)剪切为单个项目,MasterViewController将加载而不会出错。我显然做错了什么,但我不能为我的生活弄清楚它是什么。任何人都想为我指出明显的事情吗?

If I cut the list(items) down to a single item, the MasterViewController will load without errors. I'm obviously doing something wrong, but I can't for the life of me figure out what it is. Anyone care to point out the obvious for me?

推荐答案

该模板包含一个为静态单元格设置的UITableView。它实际上是一个静态单元格。 (这就是为什么让你的数组长一个项目就好了)

The template includes a UITableView that is set up for static cells. And it's actually one static cell. (this is why making your array one item long happens to kind of work)

但是你似乎不想要静态内容。所以你只需要进入故事板,选择UITableView,转到Attributes Inspector,然后将Content类型更改为Dynamic Prototypes。

But it seems you don't want static content here. So you just have to go into the storyboard, select the UITableView, go to the Attributes Inspector, and change the Content type to Dynamic Prototypes.

这应该让你超越这个问题。

That should get you past this issue.

编辑

一个有点相关的问题是你可能也想要在故事板中使用原型单元格。要做到这一点,只需将该原型的单元格标识符设置为您在tableView中使用的单元格标识符:cellForRowAtIndexPath:。

A somewhat related issue is that you probably also want to use the prototype cell in the storyboard. To do that, just set the cell identifier of that prototype to the cell identifier you are using in your tableView:cellForRowAtIndexPath:.

然后省略整个'if(单元格) ==无)'部分。单元格不会是零。

And then omit the whole 'if (cell==nil)' part. The cell won't be nil.

所以这个方法现在看起来像这样:

So that method will look like this now:

- (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;
}

希望有所帮助。

这篇关于iOS Xcode 4.2主 - 详细信息应用程序模板抛出NSRangeException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆