0

Моя проблема в том, что первая ячейка в каждом разделе работает, но если у меня есть другая ячейка в этом разделе (индекс 1), она просто сбой с этой ошибкой.NSArray objectAtIndex index 1 границы за пределами [0,0]?

Я понимаю, что этот вопрос уже задавали много раз, но я ссылаться на другие вопросы и не помогли, я получаю сообщение об ошибке:

Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 1 beyond bounds [0 .. 0]' 

и класс, где эта проблема является:

ВОТ МОЯ .Н,

#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 

ЗДЕСЬ МОЙ .М,

#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 

ответ

2

Проблема в вашем методе prepareForSegue:.

NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow]; 
UINavigationController *destViewController = (UINavigationController*)segue.destinationViewController; 
destViewController.title = [[_menuItems objectAtIndex:indexPath.row] capitalizedString]; 

Для примера выбран путь индекса является 1,2 => ваш код вызова [_menuItems objectAtIndex:2] что повышает ошибку.

Фактическое решение зависит от того, что вы хотите сделать. Если вы хотите выполнить seague только при касании на ячейки из первой секции, вы должны изменить свой didSelectRowAtIndexPath:

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

    if (indexPath.section == 0) 
     [self performSegueWithIdentifier:@"Test" sender:nil]; 

} 

В противном случае вы должны изменить destViewController.title = ... соответственно.

Совет, который может помочь вам найти такие ошибки, будет set the breakpoints on exceptions.

Смежные вопросы