My problem is that the first cell in each section works but if I have another cell in that section (index 1) it just crashes with that error.
I realize this question has been asked many times, but I have referenced the other questions and not have helped, I am getting the error:
Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 1 beyond bounds [0 .. 0]'
And the class where this problem is:
HERE IS MY .H,
#import <UIKit/UIKit.h>
#import "SWRevealViewController.h"
@interface SidebarViewController : UITableViewController
@property (nonatomic, strong) NSArray *menuItems;
@property (nonatomic, strong) NSArray *fitnessItems;
@property (nonatomic, strong) NSArray *fitnessItemsImages;
@property (nonatomic, strong) NSArray *settingsItems;
@property (nonatomic, strong) NSArray *settingsItemsImages;
@property (nonatomic, strong) NSArray *allItems;
@end
HERE IS MY .M,
#import "SidebarViewController.h"
#import "UIColor+FlatUI.h"
@interface SidebarViewController ()
@end
@implementation SidebarViewController
- (void)viewDidLoad
{
[super viewDidLoad];
_menuItems = @[@"Home"];
_fitnessItems = @[@"Find Exercises",@"Workouts",@"Build Workouts"];
_settingsItems = @[@"Account", @"Preferences"];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
switch (section) {
case 0:
return [self.menuItems count];
break;
case 1:
return [self.fitnessItems count];
break;
case 2:
return [self.settingsItems count];
break;
default:
break;
}
return 3;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
if (section == 0) {
return 0;
}
else
return 25;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 18)];
/* Create custom view to display section header... */
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 4, tableView.frame.size.width, 18)];
[label setFont:[UIFont fontWithName:@"Avenir-Light" size:15]];
label.textColor = [UIColor peterRiverColor];
/* Section header is in 0th index... */
if (section == 1) {
label.text = @"Fitness";
}
else if (section == 2) {
label.text = @"Settings";
}
else
label.text = nil;
[view addSubview:label];
[view setBackgroundColor:[UIColor cloudsColor]]; //your background color...
return view;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = @"Home";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (indexPath.section == 0) {
cell.textLabel.text = [_menuItems objectAtIndex:indexPath.row];
cell.imageView.image = [UIImage imageNamed:@"home-25.png"];
}
if (indexPath.section == 1) {
cell.textLabel.text = [_fitnessItems objectAtIndex:indexPath.row];
if (indexPath.row == 0) {
cell.imageView.image = [UIImage imageNamed:@"torso-25.png"];
}
if (indexPath.row == 1) {
cell.imageView.image = [UIImage imageNamed:@"weightlift-25.png"];
}
if (indexPath.row == 2) {
cell.imageView.image = [UIImage imageNamed:@"barbell-25.png"];
}
}
if (indexPath.section == 2) {
cell.textLabel.text = [_settingsItems objectAtIndex:indexPath.row];
if (indexPath.row == 0) {
cell.imageView.image = [UIImage imageNamed:@"user_male4-25.png"];
}
else
cell.imageView.image = [UIImage imageNamed:@"archive-25.png"];
}
UIView *bgColorView = [[UIView alloc] init];
bgColorView.backgroundColor = [UIColor cloudsColor];
bgColorView.layer.masksToBounds = YES;
[cell setSelectedBackgroundView:bgColorView];
tableView.alwaysBounceVertical = NO;
return cell;
}
- (void) prepareForSegue: (UIStoryboardSegue *) segue sender: (id) sender
{
// Set the title of navigation bar by using the menu items
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
UINavigationController *destViewController = (UINavigationController*)segue.destinationViewController;
destViewController.title = [[_menuItems objectAtIndex:indexPath.row] capitalizedString];
if ( [segue isKindOfClass: [SWRevealViewControllerSegue class]] ) {
SWRevealViewControllerSegue *swSegue = (SWRevealViewControllerSegue*) segue;
swSegue.performBlock = ^(SWRevealViewControllerSegue* rvc_segue, UIViewController* svc, UIViewController* dvc) {
UINavigationController* navController = (UINavigationController*)self.revealViewController.frontViewController;
[navController setViewControllers: @[dvc] animated: NO ];
[self.revealViewController setFrontViewPosition: FrontViewPositionLeft animated: YES];
};
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[self performSegueWithIdentifier:@"Test" sender:nil];
}
@end