NSArray objectAtIndex 索引 1 超出 [0,0] 的范围?

Posted

技术标签:

【中文标题】NSArray objectAtIndex 索引 1 超出 [0,0] 的范围?【英文标题】:NSArray objectAtIndex index 1 bounds beyond [0,0]? 【发布时间】:2013-12-12 16:45:46 【问题描述】:

我的问题是每个部分中的第一个单元格有效,但如果我在该部分中有另一个单元格(索引 1),它只会因该错误而崩溃。

我知道这个问题已经被问过很多次了,但是我参考了其他问题并没有帮助,我得到了错误:

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

还有这个问题所在的类:

这是我的 .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

这是我的 .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

【问题讨论】:

【参考方案1】:

问题出在您的 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 可能希望帮助您找到此类错误。

【讨论】:

以上是关于NSArray objectAtIndex 索引 1 超出 [0,0] 的范围?的主要内容,如果未能解决你的问题,请参考以下文章

NSArray objectAtIndex 索引 1 超出 [0,0] 的范围?

[__NSArrayM objectAtIndex:]: 索引 2 超出范围 [0 .. 1]'

NSArray objectAtIndex 不工作。请帮忙

*** 由于未捕获的异常“NSRangeException”而终止应用程序,原因:“*** -[__NSArray0 objectAtIndex:]: index 0 beyond bounds fo

从 NSArray objectAtIndex 返回的 NSString 是自动释放还是保留

数组集合与字典