“Beyond bounds” error in working with NSMutableArray

Trying desperately to find why it is that when I select a cell in my tableView and then scroll the table up and down when the cell is highlighted that the app crashes. It gives me a -[__NSArrayM objectAtIndex:]: index 6 beyond bounds [0 .. 2] error if I select either "Arts and Museums" or "Coffee and Bakeries" as an example. Both categories contain 3 values, so it makes sense I would get [0 .. 2], but I don't understand why it is happening. A total of 6 of 10 cells (1 for each category) are created when the app loads as I can see in the console. The compiler highlights this line when the error occurs: cell.textLabel.text = [arrayNo objectAtIndex:indexPath.row]; Code below...

@interface PreViewController ()

    NSMutableArray *arrayNo;


@implementation PreViewController


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];


If I were to guess .. I'd say get rid of the GestureRecognizer, and just do a reloadData after you've changed the content of the array, as a last statement of the didSelectRowAtIndexPath-method. The problem probably occurs because reloadData is never called in time, the table think it still contains 10 rows since no new call have been made to numberOfRowsInSection.

GestureRecognizers have to be allocated & added before a gesture is made, and you don't need one with table cells, since you have didSelectRowAtIndexPath.

Need Your Help

/portfolios works but /portfolio doesn't


I am new to wordpress and I've bought a theme. I've created a page with a permalink of /portfolio.

double touch on UIButton

iphone xcode ios4

How to recognize double touch on UIButton ?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.