ios4 - XML not getting parsed into tableview -
i trying parse xml data table view data not getting reflected .i implementing refresh button loads data xml once clicked. can please tell me doing wrong ? below code
// // tab1.m // sample // // created vishal thakur on 10/3/13. // copyright (c) 2013 __mycompanyname__. rights reserved. // #import "tab1.h" #import "tab2.h" #import "fruit.h" @interface tab1() { tab2 *t2; fruit *afruit; nsstring *mark; } @property (retain, nonatomic) nsarray *fruits; @end @implementation tab1 @synthesize table; @synthesize fruits; @synthesize fruitsdictionary; - (id)initwithstyle:(uitableviewstyle)style { self = [super initwithstyle:style]; if (self) { // custom initialization self.fruits = [nsarray arraywithobjects:@"apple", @"pineapple", @"orange", @"banana", @"pear", @"kiwi", @"strawberry", nil]; nsstring *filepath = [[nsbundle mainbundle] pathforresource:@"names" oftype:@"xml"]; nsxmlparser *parser = [[nsxmlparser alloc] initwithdata:[nsdata datawithcontentsoffile:filepath]]; [parser setdelegate:self]; [[uiapplication sharedapplication] setnetworkactivityindicatorvisible:no]; uiview* customview = [[uiview alloc] initwithframe:cgrectmake(0.0, 0.0, 300.0, 44.0)]; uibutton *button = [uibutton buttonwithtype:uibuttontyperoundedrect]; [button setframe:cgrectmake(50, 10, 80, 40)]; [button settitle:@"refresh" forstate:uicontrolstatenormal]; [button addtarget:self action:@selector(refreshtable:) forcontrolevents:uicontroleventtouchupinside]; [customview addsubview:button]; self.table.tableheaderview = customview; } return self; } -(void)refreshtable:(id)sender { [[uiapplication sharedapplication] setnetworkactivityindicatorvisible:yes]; nsstring *filepath = [[nsbundle mainbundle] pathforresource:@"names" oftype:@"xml"]; nsxmlparser *parser = [[nsxmlparser alloc] initwithdata:[nsdata datawithcontentsoffile:filepath]]; [parser setdelegate:self]; if ([parser parse]) { nslog(@"successful"); nslog(@"::::%@",fruitsdictionary); ///////detailscreen *ds=[[detailscreen alloc]init]; [self.table reloaddata]; } else { nslog(@"failure!"); } [[uiapplication sharedapplication] setnetworkactivityindicatorvisible:no]; } - (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section; { nsinteger count= [self.fruits count]; return count; } - (nsinteger)numberofsectionsintableview:(uitableview *)tableview; { return 1; } - (void)tableview:(uitableview *)tableview didselectrowatindexpath:(nsindexpath *)indexpath { nsarray *keys = [fruitsdictionary allkeys]; nsdictionary *local = [nsdictionary dictionarywithobject:[fruitsdictionary valueforkey:[keys objectatindex:indexpath.row]]forkey:[keys objectatindex:indexpath.row]]; nslog(@"%@",keys); uialertview *alert = [[uialertview alloc] initwithtitle:@"fruit" message:(nsstring *)[keys objectatindex:indexpath.row] delegate:self cancelbuttontitle:@"cancel" otherbuttontitles:@"other", nil]; [alert show]; } - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { static nsstring *cellidentifier = @"cell identifier"; uitableviewcell * cell = [tableview dequeuereusablecellwithidentifier:cellidentifier]; if (!cell) { cell = [[uitableviewcell alloc] initwithstyle:uitableviewcellstyledefault reuseidentifier:cellidentifier]; } nsarray *keys = [fruitsdictionary allkeys]; nsstring *fruit = [keys objectatindex:[indexpath row]]; [cell.textlabel settext:fruit]; return cell; } - (void)parser:(nsxmlparser *)parser didstartelement:(nsstring *)elementname namespaceuri:(nsstring *)namespaceuri qualifiedname:(nsstring *)qname attributes:(nsdictionary *)attributedict{ if ([elementname isequaltostring:@"fruit"]) { afruit = [[fruit alloc] init]; if (!fruitsdictionary) { fruitsdictionary = [[nsmutabledictionary alloc] init]; } } } - (void)parser:(nsxmlparser *)parser foundcharacters:(nsstring *)string { currentelement = nil; currentelement = [[nsmutablestring alloc] initwithstring:string]; } - (void)parser:(nsxmlparser *)parser didendelement:(nsstring *)elementname namespaceuri:(nsstring *)namespaceuri qualifiedname:(nsstring *)qname { if ([elementname isequaltostring:@"name"]) { afruit.name = currentelement; } else if ([elementname isequaltostring:@"description"]) { afruit.description = currentelement; } else if ([elementname isequaltostring:@"fruit"]) { [fruitsdictionary setobject:afruit forkey:afruit.name]; } } - (void)didreceivememorywarning { // releases view if doesn't have superview. [super didreceivememorywarning]; // release cached data, images, etc aren't in use. } #pragma mark - view lifecycle - (void)viewdidload { [super viewdidload]; // uncomment following line preserve selection between presentations. // self.clearsselectiononviewwillappear = no; // uncomment following line display edit button in navigation bar view controller. // self.navigationitem.rightbarbuttonitem = self.editbuttonitem; } - (void)viewdidunload { [self settable:nil]; [self setfruits:nil]; [super viewdidunload]; // release retained subviews of main view. // e.g. self.myoutlet = nil; } - (void)viewwillappear:(bool)animated { [super viewwillappear:animated]; } - (void)viewdidappear:(bool)animated { [super viewdidappear:animated]; } - (void)viewwilldisappear:(bool)animated { [super viewwilldisappear:animated]; } - (void)viewdiddisappear:(bool)animated { [super viewdiddisappear:animated]; } - (bool)shouldautorotatetointerfaceorientation:(uiinterfaceorientation)interfaceorientation { // return yes supported orientations return (interfaceorientation == uiinterfaceorientationportrait); } #pragma mark - table view data source /* // 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:[nsarray arraywithobject: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 @end
you can use parserdidstartdocument:
method doing this.
according nsxmlparserdelegate:
parserdidenddocument
:sent parser object delegate when has completed parsing.
- (void)parserdidenddocument:(nsxmlparser *)parser
parameters
parser
a parser object.
availability
available in ios 2.0 , later. available part of informal protocol prior ios 4.0.
see also
– parserdidstartdocument:
declared in nsxmlparser.h
you can implement like:
- (void)parserdidstartdocument:(nsxmlparser *)parser { [self.table reloaddata]; }
Comments
Post a Comment