iPhone: UITableView not refreshing -
i have read many topics uitableviews not refreshing on iphone, couldn't find matching situation, i'm asking help.
in class, extends uiviewcontroller, have tableview , 'elementlist' (which wrapper nsmutablearray) used data source.
a separate thread adds new element array via 'updatelist:' method. when happens, want tableview refreshed automatically, doesn't happen.
by debugging app, can see 'cellforrowatindexpath' never called , can't figure out why.
i tried add observer, calls 'reloadtableview' method (it called) tableview not updated.
this code:
#import <uikit/uikit.h> @interface listviewcontroller : uiviewcontroller <uitableviewdelegate, uitableviewdatasource> { uitableview *tableview; elementlist *elementlist; // wrapper nsmutablearray } // called else, triggers whole thing -(void)updatelist:(element *)element; // added out of desperation -(void)reloadtableview; @end @implementation listviewcontroller -(void)loadview { // create tableview tableview = [[uitableview alloc] initwithframe:[[uiscreen mainscreen] applicationframe] style:uitableviewstyleplain]; assert(tableview != nil); tableview.delegate = self; tableview.datasource = self; [tableview reloaddata]; self.view = tableview; // added out of desperation [[nsnotificationcenter defaultcenter] addobserver:self selector:@selector(reloadtableview) name:@"updatedlistnotification" object:nil]; } -(void)reloadtableview { // try [tableview reloaddata]; [tableview setneedslayout]; [tableview setneedsdisplay]; [tableview reloaddata]; [self.view setneedslayout]; [self.view setneedsdisplay]; } // called else, triggers whole thing -(void)updatelist:(element *)element { assert(element != nil); [elementlist addelement:element]; [element release]; // notify observer, should update table view [[nsnotificationcenter defaultcenter] postnotificationname:@"updatedlistnotification" object:self]; } // tableview delegate protocol - (void)tableview:(uitableview *)table didselectrowatindexpath:(nsindexpath *)indexpath { element *selected_element = [elementlist getelementatindex:indexpath.row]; if (selected_element == nil) { nslog(@"error: selected invalid element"); return; } [table deselectrowatindexpath:indexpath animated:yes]; nslog(@"selected %@", selected_element.name); } // tableview data source protocol - (nsinteger)numberofsectionsintableview:(uitableview *)tableview { return 1; } - (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section { return [elementlist count]; } - (uitableviewcell *)tableview:(uitableview *)table cellforrowatindexpath:(nsindexpath *)indexpath { static nsstring *cellidentifier = @"cell"; uitableviewcell *cell = [table dequeuereusablecellwithidentifier:cellidentifier]; if (cell == nil) { cell = [[[uitableviewcell alloc] initwithstyle:uitableviewcellstyledefault reuseidentifier:cellidentifier] autorelease]; } // set cell label cell.textlabel.text = [[elementlist getelementatindex:indexpath.row] name]; cell.textlabel.frame = cell.bounds; cell.textlabel.textalignment = uitextalignmentleft; return cell; } @end
any appreciated, thank you.
notifications executed in same thread caller. updating ui should done in main thread, should call -reloaddata on main thread:
-(void)updatelist:(element *)element { assert(element != nil); [elementlist addelement:element]; [self.tableview performselectoronmainthread:@selector(reloaddata) withobject:nil]; }
also note shouldn't release object don't own. don't call [element release] in -updatelist method. release should called caller of function.
Comments
Post a Comment