uitableview - "Beyond bounds" error in working with NSMutableArray -
trying desperately find why when select cell in tableview , scroll table , down when cell highlighted app crashes. gives me -[__nsarraym objectatindex:]: index 6 beyond bounds [0 .. 2]
error if select either "arts , museums" or "coffee , bakeries" example. both categories contain 3 values, makes sense [0 .. 2]
, don't understand why happening. total of 6 of 10 cells (1 each category) created when app loads can see in console. compiler highlights line when error occurs: cell.textlabel.text = [arrayno objectatindex:indexpath.row];
code below...
@interface previewcontroller () { nsmutablearray *arrayno; } @end @implementation previewcontroller -(void)viewdidappear:(bool)animated { nsstring *arts = @"arts , museums"; nsstring *coffee = @"coffee , bakeries"; nsstring *tours = @"tours , festivals"; nsstring *hotels = @"hotels , inns"; nsstring *leisure = @"leisure , recreation"; nsstring *music = @"live music"; nsstring *bars = @"night clubs , 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 , 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 , 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 , festivals"]) { nsstring *festivals = @"food , drink festivals"; [arrayno removeallobjects]; [arrayno addobject:festivals]; } if ([cell.textlabel.text isequaltostring: @"hotels , inns"]) { nsstring *breakfasts = @"bed , 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 , 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 , 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 , 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 guess .. i'd rid of gesturerecognizer, , reloaddata after you've changed content of array, last statement of didselectrowatindexpath-method. problem occurs because reloaddata never called in time, table think still contains 10 rows since no new call have been made numberofrowsinsection.
gesturerecognizers have allocated & added before gesture made, , don't need 1 table cells, since have didselectrowatindexpath.
Comments
Post a Comment