拼命地试图找出为什么当我在 tableView 中选择一个单元格然后当单元格突出显示时上下滚动表格时应用程序崩溃。-[__NSArrayM objectAtIndex:]: index 6 beyond bounds [0 .. 2]
如果我选择“艺术和博物馆”或“咖啡和面包店”作为示例,它会给我一个错误。这两个类别都包含 3 个值,所以我会得到有意义的[0 .. 2]
,但我不明白它为什么会发生。正如我在控制台中看到的那样,当应用程序加载时,总共创建了 10 个单元中的 6 个(每个类别 1 个)。发生错误时,编译器会突出显示这一行:cell.textLabel.text = [arrayNo objectAtIndex:indexPath.row];
下面的代码...
@interface PreViewController ()
{
NSMutableArray *arrayNo;
}
@end
@implementation PreViewController
-(void)viewDidAppear:(BOOL)animated
{
NSString *arts = @"Arts and Museums";
NSString *coffee = @"Coffee and Bakeries";
NSString *tours = @"Tours and Festivals";
NSString *hotels = @"Hotels and Inns";
NSString *leisure = @"Leisure and Recreation";
NSString *music = @"Live Music";
NSString *bars = @"Night Clubs and Bars";
NSString *food = @"Restaurants";
NSString *shopping = @"Shopping";
NSString *transportation = @"Transportation";
[arrayNo addObject:arts];
[arrayNo addObject:coffee];
[arrayNo addObject:tours];
[arrayNo addObject:hotels];
[arrayNo addObject:leisure];
[arrayNo addObject:music];
[arrayNo addObject:bars];
[arrayNo addObject:food];
[arrayNo addObject:shopping];
[arrayNo addObject:transportation];
[[self myTableView] reloadData];
}
- (void)viewDidLoad
{
[super viewDidLoad];
arrayNo = [[NSMutableArray alloc] init];
[[self myTableView] setDelegate:self];
[[self myTableView] setDataSource:self];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [arrayNo count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell)
{
NSLog(@"CREATING NEW CELL");
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.textLabel.textColor = [UIColor whiteColor];
cell.textLabel.textAlignment = NSTextAlignmentCenter;
cell.textLabel.textColor = [UIColor colorWithRed:(100/255.0) green:(130/255.0) blue:(255/255.0) alpha:1.0];
}
cell.textLabel.text = [arrayNo objectAtIndex:indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (cell)
{
UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(reLoad)];
tapped.numberOfTapsRequired = 1;
[cell addGestureRecognizer:tapped];
}
if ([cell.textLabel.text isEqualToString: @"Arts and Museums"])
{
NSString *galleries = @"Art Galleries";
NSString *dramatic = @"Dramatic Arts";
NSString *museums = @"Museums";
[arrayNo removeAllObjects];
[arrayNo addObject:galleries];
[arrayNo addObject:dramatic];
[arrayNo addObject:museums];
}
if ([cell.textLabel.text isEqualToString: @"Coffee and Bakeries"])
{
NSString *bakeries = @"Bakeries";
NSString *cafes = @"Cafés";
NSString *shops = @"Coffee Shops";
[arrayNo removeAllObjects];
[arrayNo addObject:bakeries];
[arrayNo addObject:cafes];
[arrayNo addObject:shops];
}
if ([cell.textLabel.text isEqualToString: @"Tours and Festivals"])
{
NSString *festivals = @"Food and Drink Festivals";
[arrayNo removeAllObjects];
[arrayNo addObject:festivals];
}
if ([cell.textLabel.text isEqualToString: @"Hotels and Inns"])
{
NSString *breakfasts = @"Bed and Breakfasts";
NSString *hotels = @"Hotels";
NSString *inns = @"Inns";
NSString *motels = @"Motels";
[arrayNo removeAllObjects];
[arrayNo addObject:breakfasts];
[arrayNo addObject:hotels];
[arrayNo addObject:inns];
[arrayNo addObject:motels];
}
if ([cell.textLabel.text isEqualToString: @"Leisure and Recreation"])
{
NSString *arcades = @"Arcades";
NSString *beaches = @"Beaches";
NSString *bowling = @"Bowling";
NSString *breweries = @"Breweries";
NSString *campgrounds = @"Campgrounds";
NSString *cinemas = @"Cinemas";
NSString *climbing = @"Climbing";
NSString *parks = @"Parks";
NSString *ski = @"Ski Resorts";
NSString *spa = @"Spa Resorts";
NSString *water = @"Water Rentals";
[arrayNo removeAllObjects];
[arrayNo addObject:arcades];
[arrayNo addObject:beaches];
[arrayNo addObject:bowling];
[arrayNo addObject:breweries];
[arrayNo addObject:campgrounds];
[arrayNo addObject:cinemas];
[arrayNo addObject:climbing];
[arrayNo addObject:parks];
[arrayNo addObject:ski];
[arrayNo addObject:spa];
[arrayNo addObject:water];
}
if ([cell.textLabel.text isEqualToString: @"Live Music"])
{
NSString *bars = @"Bars";
NSString *clubs = @"Clubs";
NSString *restaurants = @"Restaurants";
NSString *theaters = @"Theaters";
[arrayNo removeAllObjects];
[arrayNo addObject:bars];
[arrayNo addObject:clubs];
[arrayNo addObject:restaurants];
[arrayNo addObject:theaters];
}
if ([cell.textLabel.text isEqualToString: @"Night Clubs and Bars"])
{
NSString *bars = @"Bars";
NSString *lounges = @"Lounges";
NSString *clubs = @"Night Clubs";
[arrayNo removeAllObjects];
[arrayNo addObject:bars];
[arrayNo addObject:lounges];
[arrayNo addObject:clubs];
}
if ([cell.textLabel.text isEqualToString: @"Restaurants"])
{
NSString *asian = @"Asian";
NSString *fast = @"Fast Food";
NSString *french = @"French";
NSString *german = @"German";
NSString *grill = @"Grill and Variety";
NSString *indian = @"Indian";
NSString *italian = @"Italian";
NSString *mexican = @"Mexican";
NSString *eastern = @"Middle Eastern";
NSString *seafood = @"Seafood";
[arrayNo removeAllObjects];
[arrayNo addObject:asian];
[arrayNo addObject:fast];
[arrayNo addObject:french];
[arrayNo addObject:german];
[arrayNo addObject:grill];
[arrayNo addObject:indian];
[arrayNo addObject:italian];
[arrayNo addObject:mexican];
[arrayNo addObject:eastern];
[arrayNo addObject:seafood];
}
if ([cell.textLabel.text isEqualToString: @"Shopping"])
{
NSString *art = @"Art Supplies";
NSString *books = @"Books";
NSString *candy = @"Candy";
NSString *cooking = @"Cooking";
NSString *electronics = @"Electronics";
NSString *apparel = @"Apparel";
NSString *florists = @"Florists";
NSString *grocery = @"Grocery";
NSString *health = @"Health";
NSString *home = @"Home";
NSString *jewlery = @"Jewelry";
NSString *music = @"Music";
NSString *outdoor = @"Outdoor Gear";
NSString *photography = @"Photography";
NSString *souvenirs = @"Souvenirs";
NSString *sports = @"Sports";
[arrayNo removeAllObjects];
[arrayNo addObject:apparel];
[arrayNo addObject:art];
[arrayNo addObject:books];
[arrayNo addObject:candy];
[arrayNo addObject:cooking];
[arrayNo addObject:electronics];
[arrayNo addObject:florists];
[arrayNo addObject:grocery];
[arrayNo addObject:health];
[arrayNo addObject:home];
[arrayNo addObject:jewlery];
[arrayNo addObject:music];
[arrayNo addObject:outdoor];
[arrayNo addObject:photography];
[arrayNo addObject:souvenirs];
[arrayNo addObject:sports];
}
if ([cell.textLabel.text isEqualToString: @"Transportation"])
{
NSString *airports = @"Airports";
NSString *bicycle = @"Bicycle Rentals";
NSString *bus = @"Bus Lines";
NSString *ferries = @"Ferries";
NSString *taxis = @"Taxis";
NSString *trains = @"Trains";
NSString *rentals = @"Vehicle Rentals";
[arrayNo removeAllObjects];
[arrayNo addObject:airports];
[arrayNo addObject:bicycle];
[arrayNo addObject:bus];
[arrayNo addObject:ferries];
[arrayNo addObject:taxis];
[arrayNo addObject:trains];
[arrayNo addObject:rentals];
}
}
- (void) reLoad
{
[self.myTableView reloadData];
}