gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 22:20:31 25 4
gpt4 key购买 nike

我的问题是每个部分中的第一个单元格都有效,但如果我在该部分(索引 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

最佳答案

问题出在您的 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 .

关于ios - NSArray objectAtIndex 索引 1 超出 [0,0] 范围?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20549230/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com