scroll - UITableview keeps old cells after reloadData -
i have view tabbar @ bottom , tableview. when barbuttonitem pressed, array holds data tableview changes.
with nslogs, clear array changing, [nsarray count] displays different values. however, when use [uitableview reloaddata], cells stay same.
if scroll bit , scroll down, whatever went offscreen gets updating. i'm guessing because when goes offscreen dequeued , when comes redrawn new data. there way have redraw everything?
#import "tableviewcontroller.h" @interface tableviewcontroller () @end @implementation tableviewcontroller @synthesize listbar,selectedtab , linktableview, barbutton5, barbutton4, barbutton3, barbutton2, barbutton1, lists, imageid, titleid, linkid, cells; - (void)viewdidload { [super viewdidload]; linktableview = [[uitableview alloc] init]; lists = [[nsarray alloc] init]; imageid = [[nsmutablearray alloc] init]; titleid = [[nsmutablearray alloc] init]; linkid = [[nsmutablearray alloc] init]; linktableview.delegate = self; linktableview.datasource = self; } -(void)viewwillappear:(bool)animated{ nsarray *baritems = [[nsarray alloc] initwithobjects:barbutton1, barbutton2, barbutton3, barbutton4, barbutton5, nil]; listbar.selecteditem = [baritems objectatindex:selectedtab]; //when view appear load data dependent on selectedtab [self performselector:@selector(updatetable)]; } - (void)didreceivememorywarning { [super didreceivememorywarning]; // dispose of resources can recreated. } -(void)tabbar:(uitabbar *)tabbar didselectitem:(uitabbaritem *)item{ selectedtab = item.tag; [self performselector:@selector(updatetable)]; } -(void)updatetable{ [imageid removeallobjects]; [titleid removeallobjects]; [linkid removeallobjects]; //load xml file rxmlelement *rxml = [rxmlelement elementfromxmlfile:@"lists.xml"]; //makes array list children lists = [rxml children:@"list"]; cells = [[lists objectatindex:selectedtab] children:@"cell"]; [rxml iterateelements:cells usingblock:^(rxmlelement *cellelement) { [imageid addobject:[cellelement child:@"imagename"]]; [titleid addobject:[cellelement child:@"celltext" ]]; [linkid addobject:[cellelement child:@"link" ]]; }]; nslog(@"count %i", [cells count]); [linktableview reloaddata]; } #pragma mark - table view data source - (nsinteger)numberofsectionsintableview:(uitableview *)tableview { // return number of sections. return 1; } - (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section { // return number of rows in section. return [cells count]; } -(cgfloat)tableview:(uitableview *)tableview heightforrowatindexpath:(nsindexpath *)indexpath{ return 60; } - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { static nsstring *cellidentifier = @"linkcell"; linkcell *cell = [tableview dequeuereusablecellwithidentifier:cellidentifier forindexpath:indexpath]; if (cell == nil) { cell = [[linkcell alloc] initwithstyle:uitableviewcellstyledefault reuseidentifier:cellidentifier]; } // configure cell... nsstring *imagename = [nsstring stringwithformat:@"%@", [imageid objectatindex:indexpath.row]]; cell.image.image = [uiimage imagenamed:imagename]; nsstring *labeltext = [nsstring stringwithformat:@"%@", [titleid objectatindex:indexpath.row]]; cell.linkcelllabel.text = labeltext; return cell; } /* // override support conditional editing of table view. - (bool)tableview:(uitableview *)tableview caneditrowatindexpath:(nsindexpath *)indexpath { // return no if not want specified item editable. return yes; } */ /* // override support editing table view. - (void)tableview:(uitableview *)tableview commiteditingstyle:(uitableviewcelleditingstyle)editingstyle forrowatindexpath:(nsindexpath *)indexpath { if (editingstyle == uitableviewcelleditingstyledelete) { // delete row data source [tableview deleterowsatindexpaths:@[indexpath] withrowanimation:uitableviewrowanimationfade]; } else if (editingstyle == uitableviewcelleditingstyleinsert) { // create new instance of appropriate class, insert array, , add new row table view } } */ /* // override support rearranging table view. - (void)tableview:(uitableview *)tableview moverowatindexpath:(nsindexpath *)fromindexpath toindexpath:(nsindexpath *)toindexpath { } */ /* // override support conditional rearranging of table view. - (bool)tableview:(uitableview *)tableview canmoverowatindexpath:(nsindexpath *)indexpath { // return no if not want item re-orderable. return yes; } */ #pragma mark - table view delegate - (void)tableview:(uitableview *)tableview didselectrowatindexpath:(nsindexpath *)indexpath { // navigation logic may go here. create , push view controller. /* <#detailviewcontroller#> *detailviewcontroller = [[<#detailviewcontroller#> alloc] initwithnibname:@"<#nib name#>" bundle:nil]; // ... // pass selected object new view controller. [self.navigationcontroller pushviewcontroller:detailviewcontroller animated:yes]; */ } @end
remove line code:
linktableview = [[uitableview alloc] init];
or other initializers uitableview , use code update sections
[self.tableview beginupdates]; nsmutableindexset* index = [[nsmutableindexset alloc]init]; [index addindex:0]; [self.tableview reloadsections:index withrowanimation:uitableviewrowanimationautomatic]; [self.tableview endupdates];
Comments
Post a Comment