iOS Xcode 4.2 Master-Detail Application Template Throwing NSRangeException

断了今生、忘了曾经 提交于 2019-12-24 12:08:37

问题


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?


回答1:


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:

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

Hope that helps.



来源:https://stackoverflow.com/questions/8410522/ios-xcode-4-2-master-detail-application-template-throwing-nsrangeexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!