2 Replies Latest reply: Jul 11, 2012 10:47 PM by Llessur999
gmazza Level 1 Level 1 (0 points)

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!!

  • gmazza Level 1 Level 1 (0 points)

    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

    {

        [super viewDidLoad];

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

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

        [self setTitle:@"Happy Appy"];

       

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

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

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

    }

     

    // Return the number of rows in the section.

        return HappyArray.count;

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

    {

        MoodTableViewController *MVC = [[MoodTableViewController alloc] 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.navigationController pushViewController:MVC animated:YES];


     

    table view .h


    {  

    int happyInt;

    }

     

    @property (nonatomic, retain) NSMutableArray *DogArray;

    @property (nonatomic, retain) NSMutableArray *CatArray;

    @property (nonatomic, retain) NSMutableArray *SnakeArray;

    @property int happyInt;

     

    table view .m:

     

    #import "MoodTableViewController.h"

     

    @interface MoodTableViewController ()

     

    @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

        }

        return self;

    }

     

    - (void)viewDidLoad

    {

       

        MoodArray = [[NSMutableArray alloc]

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

        DayArray = [[NSMutableArray alloc]

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

        LocationArray = [[NSMutableArray alloc]

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

     

                    

        [super viewDidLoad];

      

    }

     

    - (void)viewDidUnload

    {

       // [self setMoodDetails:nil];

        [super viewDidUnload];

        // 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.tableView reloadData];

       

    }

     

    - (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 = [[UITableViewCell alloc] initWithStyleUITableViewCellStyleDefault) 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



  • Llessur999 Level 4 Level 4 (1,190 points)

    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.