Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Help with code for an array

Hey there,

I am super new to xcode and am having errors with my app. Its builds successfully but errors soon after:

reason: '*** -[__NSArrayI objectAtIndex:]: index 3 beyond bounds


I have a Root View Controller that reads 3 values an array.

I have a table view controller that has 3 arrays implemented in it's class so whatever choice is made from the root view controller, the corresponding array will be displayed in the table view controller.

I got the code after watching this video, but its done with the latest xcode that has storyboards so I don't know whats wrong.

http://www.youtube.com/watch?v=JGMa3eLGxXQ


My code is the exact same as the video and my table view controller has 7 prototype cells.


Please let me know what needs to be changed in my code considering I have the lates xcode, 4.3


Let me know if I need to provide more info, thanks in advance!!

Posted on Jul 8, 2012 11:34 AM

Reply
2 replies

Jul 8, 2012 3:30 PM in response to gmazza

here is my code for each view controller:


the root view .h:

@interface HappyRootViewController : UITableViewController

@property (nonatomic, retain) NSMutableArray *HappyArray;


root view .m:

@implementation HappyRootViewController


@synthesize HappyArray;


- (void)viewDidLoad

{

[superviewDidLoad];


// Do any additional setup after loading the view, typically from a nib.

self.HappyArray = [NSMutableArrayarrayWithObjects:@"Dog?",@"Cat?",@"Snake?", nil];

[selfsetTitle:@"Happy Appy"];

[self.HappyArray addObject:@"Dog"];

[self.HappyArray addObject:@"Cat"];

[self.HappyArrayaddObject:@"Snake"];

}


// Return the number of rows in the section.

returnHappyArray.count;

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

MoodTableViewController *MVC = [[MoodTableViewControlleralloc] initWithNibName:@"MoodTableViewController"bundle:nil];

if ([[HappyArray objectAtIndex:indexPath.row] isEqual:@"Dog"]) {

MVC.happyInt = 0;

[MVC setTitle:[HappyArray objectAtIndex:indexPath.row]];

}

if ([[HappyArray objectAtIndex:indexPath.row] isEqual:@"Cat"]) {

MVC.happyInt = 1;

[MVC setTitle:[HappyArray objectAtIndex:indexPath.row]];

}

if ([[HappyArray objectAtIndex:indexPath.row] isEqual:@"Snake"]) {

MVC.happyInt = 2;

[MVC setTitle:[HappyArray objectAtIndex:indexPath.row]];

}

[self.navigationControllerpushViewController:MVC animated:YES];


table view .h

{

int happyInt;

}


@property (nonatomic, retain) NSMutableArray *DogArray;

@property (nonatomic, retain) NSMutableArray *CatArray;

@property (nonatomic, retain) NSMutableArray *SnakeArray;

@propertyint happyInt;


table view .m:


#import "MoodTableViewController.h"


@interfaceMoodTableViewController ()


@end


@implementation MoodTableViewController

//@synthesize MoodNumber, MoodName;


@synthesize happyInt;

@synthesize DogArray;

@synthesize CatArray;

@synthesize SnakeArray;


- (id)initWithStyle:(UITableViewStyle)style

{

self = [super initWithStyle:style];

if (self) {

// Custom initialization

}

returnself;

}


- (void)viewDidLoad

{

MoodArray = [[NSMutableArrayalloc]

initWithObjects:@"Dog 1", @"Dog 2", nil];

DayArray = [[NSMutableArrayalloc]

initWithObjects:@"Cat 1", @"Cat 2", @"Cat 3", @"Cat 4",nil];

LocationArray = [[NSMutableArrayalloc]

initWithObjects:@"Snake 1", @"Snake 2", nil];


[superviewDidLoad];

}


- (void)viewDidUnload

{

// [self setMoodDetails:nil];

[superviewDidUnload];

// Release any retained subviews of the main view.

// e.g. self.myOutlet = nil;

}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

{

return (interfaceOrientation == UIInterfaceOrientationPortrait);

}



#pragma mark - Table view data source


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

// Return the number of sections.

return 1;

}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

// Return the number of rows in the section.

if (happyInt == 0)

return [MoodArray count];

if (happyInt == 1)

return [DayArray count];

if (happyInt == 2)

return [LocationArray count];

[self.tableViewreloadData];

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

//Create an NSString object that we can use as the reuse identifier

static NSString *CellIdentifier = @"Cell";

//Check to see if we can reuse a cell from a row that has just rolled off the screen

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

//If there are no cells to be reused, create a new cell

if (cell == nil) {

cell = [[UITableViewCellalloc] initWithStyle😟UITableViewCellStyleDefault) reuseIdentifier:CellIdentifier];

//Configure the cell

if (happyInt == 0)

cell.textLabel.text = [MoodArray objectAtIndex:indexPath.row];

if (happyInt == 1)

cell.textLabel.text = [DayArray objectAtIndex:indexPath.row];

if (happyInt == 2)

cell.textLabel.text = [LocationArray objectAtIndex:indexPath.row];

[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];

//Set the text attribute to whatever we are currently looking at in our array

//cell.textLabel.text = [MoodArray objectAtIndex:indexPath.row];

//Set the detail disclosure indicator but not for this as its a detail

//cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

//Return the cell

return cell;

}


/*

// Override to support conditional editing of the table view.

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath

{

// Return NO if you do not want the specified item to be editable.

return YES;

}

*/


/*


// Override to support conditional rearranging of the table view.

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath

{

// Return NO if you do not want the item to be re-orderable.

return YES;

}

*/


#pragma mark - Table view delegate

/*

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

// Navigation logic may go here. Create and push another view controller.

//HappyRootViewController *HappyRVC = [[HappyRootViewController alloc] initWithNibName:@"<#Nib name#>" bundle:nil];

// ...

// Pass the selected object to the new view controller.

//[self.navigationController pushViewController:HappyRVC animated:YES];

UITableViewCell *thisCell = [tableView cellForRowAtIndexPath:indexPath];

if (thisCell.accessoryType == UITableViewCellAccessoryNone) {

thisCell.accessoryType = UITableViewCellAccessoryCheckmark;

}

else

{

thisCell.accessoryType = UITableViewCellAccessoryNone;

}

}


- (UITableViewCellAccessoryType)tableView:(UITableView *)tableView accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath {

//add your own code to set the cell accesory type.

return UITableViewCellAccessoryNone;

}

*/

}

@end

Jul 11, 2012 10:47 PM in response to gmazza

Are you really pushing a new view controller in cellForRowAtIndexPath? That seems way wrong. I suspect you mean to be pushing in didSelectRowAtIndexPath. Or this is a copy/paste error.


In your first view controller, you are initializing the array with 6 items. So you should be providing content for rows 0 through 5 in cellForRowAtIndexPath.

Help with code for an array

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.