iOS Xcode 4.2 主从应用程序模板抛出 NSRangeException
这里是新手问题...
我已经使用 ARC 和 Storyboard 在 Xcode 4.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 (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;
}
当程序运行时,此代码将在此行失败:
[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将正确加载。我显然做错了什么,但我一生都无法弄清楚它是什么。有人愿意为我指出显而易见的事情吗?
Newbie question here ...
I've created a Master-Detail Application project in Xcode 4.2 using ARC and Storyboard. I've modified the template to be:
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 (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];
with this exception:
*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 1 beyond bounds [0 .. 0]'
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该模板包含一个为静态单元格设置的 UITableView。它实际上是一个静态单元。 (这就是为什么让你的数组中的一项很长是一种工作)
但是你似乎不想要这里的静态内容。因此,您只需进入故事板,选择 UITableView,转到属性检查器,然后将内容类型更改为动态原型。
这应该可以帮助你解决这个问题。
编辑
一个有些相关的问题是您可能还想在故事板中使用原型单元。为此,只需将该原型的单元格标识符设置为您在 tableView:cellForRowAtIndexPath: 中使用的单元格标识符。
然后省略整个“if (cell==nil)”部分。单元格不会为零。
所以该方法现在看起来像这样:
希望有帮助。
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)
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.
EDIT
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:.
And then omit the whole 'if (cell==nil)' part. The cell won't be nil.
So that method will look like this now:
Hope that helps.