拼命寻找原因,当我在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];
}
答案 0 :(得分:0)
如果我猜测..我会说摆脱GestureRecognizer,只是在你改变了数组的内容后再做一个reloadData,作为didSelectRowAtIndexPath方法的最后一个声明。问题可能是因为reloadData永远不会及时调用,表认为它仍然包含10行,因为没有对numberOfRowsInSection进行新的调用。
必须分配GestureRecognizers&在做出手势之前添加,并且您不需要具有表格单元格的手势,因为您有didSelectRowAtIndexPath。