iphone - UITableView is loading the same cells of some reason -
iphone - UITableView is loading the same cells of some reason -
i have tableview loading custom cell , loading info json string on server. json string parsed array of 14 ids (id_array).
if cell==nil, i'm using [id_array objectatindex:indexpath.row] id , fetch more info row server, , set cell's labels , image.
when running app, uitableview loading visible rows [0,1,2,3,4] (cell height 70px). when scrolling downwards tableview, row [5] loaded , fetching info server, problem beyond point - tableview repeating 6 rows instead of requesting new info server new rows...
but request new info row [5], not visible (and not loaded) when app first runs.
anyone have thought why happening? thanks!
edit: here cellforrowatindexpath method
- (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { static nsstring *cellidentifier = @"customcell"; customcell *cell = (customcell *)[tableview dequeuereusablecellwithidentifier:cellidentifier]; if (cell == nil) { nsarray *toplevelobjects = [[nsbundle mainbundle] loadnibnamed:@"customcell" owner:self options:nil]; (id currentobject in toplevelobjects) { if ([currentobject iskindofclass:[uitableviewcell class]]) { cell = (customcell *)currentobject; nsstring *appsurl = [nsstring stringwithformat:@"http://myapi.com?id=%@",[app_ids objectatindex:indexpath.row]]; nslog(@"row -> %d | id -> %@",indexpath.row,[app_ids objectatindex:indexpath.row]); nsurlrequest *request = [nsurlrequest requestwithurl:[nsurl urlwithstring:appsurl]]; [cell updateappinfoforcellwithrequest:request]; break; } } } // configure cell... homecoming cell; }
if setting info if cell==nil, issue. uitable builds cache of table view cells, creating new 1 if cell nil. therefore, must set info each time, i.e. outside of cell==nil block.
the illustration below shows process. first, grab cell pool, if there isn't free cell, create new one. set cell's values appropriate row.
- (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { static nsstring *cellidentifier = @"cell"; uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:cellidentifier]; if (cell == nil) { cell = [[[uitableviewcell alloc] initwithstyle:uitableviewcellstyledefault reuseidentifier:cellidentifier] autorelease]; } id somedata = [id_array objectatindex:indexpath.row] cell.textlabel.text = [somedata somestring]; homecoming cell; } iphone uitableview
Comments
Post a Comment