我的问题是每个部分中的第一个单元格有效,但如果我在该部分中有另一个单元格(索引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
答案 0 :(得分:2)
问题在于您的prepareForSegue:
方法。
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
UINavigationController *destViewController = (UINavigationController*)segue.destinationViewController;
destViewController.title = [[_menuItems objectAtIndex:indexPath.row] capitalizedString];
例如,选择的索引路径是1.2 =&gt;您的代码正在调用引发错误的[_menuItems objectAtIndex:2]
。
实际解决方案取决于您想要做什么。如果您只想在点击第一部分的单元格时执行此操作,则应更改didSelectRowAtIndexPath:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.section == 0)
[self performSegueWithIdentifier:@"Test" sender:nil];
}
否则您应该相应地更改destViewController.title = ...
。
可能希望帮助您找到此类错误的提示是set the breakpoints on exceptions。