Saturday, 15 February 2014

ios - how to fetch the UITableViewCell row value to UIButton action method -



ios - how to fetch the UITableViewCell row value to UIButton action method -

hi in application i'm fetching info server , displaying in uitableviewcell , have dynamic uibuttons in uitableview . want uitableviewcell info uibutton action method.

my code fetch server data.

-(void) retrievedata { nsurl * url = [nsurl urlwithstring:getdataurl]; nsdata * info = [nsdata datawithcontentsofurl:url]; id json = [nsjsonserialization jsonobjectwithdata:data options:kniloptions error:nil]; menuarray = [[nsmutablearray alloc]init]; menuarray1=[nsmutablearray array]; nsarray * resparray = [json objectforkey:@"nonveg_biriyani"]; (int i=0; i<resparray.count; i++) { nsstring * pal = [[resparray objectatindex:i]objectforkey:@"name"]; nsstring * tll = [[resparray objectatindex:i]objectforkey:@"cost"]; [menuarray addobject:pal]; [menuarray1 addobject:tll]; } [self.mytableview reloaddata]; }

my uitableviewcell code.

-(nsinteger)numberofsectionsintableview:(uitableview *)tableview{ homecoming 1; } -(nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section{ homecoming menuarray.count; } -(uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath{ static nsstring *cellidentifier =@"cell"; meunucell *cell =(meunucell *)[tableview dequeuereusablecellwithidentifier:cellidentifier]; if (cell == nil) { cell = [[meunucell alloc]initwithstyle:uitableviewcellstyledefault reuseidentifier:cellidentifier]; } cell.name.text=[menuarray objectatindex:indexpath.row]; cell.ctt.text=[menuarray1 objectatindex:indexpath.row]; button2 = [uibutton buttonwithtype:uibuttontyperoundedrect]; button2.frame = cgrectmake(280,20, 40.0, 40.0); [button2 settitle:@"add" forstate:uicontrolstatenormal]; [button2 addtarget:self action:@selector(amethod2:) forcontrolevents:uicontroleventtouchupinside]; button1.tag=indexpath.row; [cell.contentview addsubview:button2]; } homecoming cell; }

uibutton action method.

-(void)amethod2:(id)sender { int tag=[sender tag]; nsstring *item=[self.menuarray objectatindex:tag]; nslog(@"%@",item); nsstring *cost=[self.menuarray1 objectatindex:tag]; nslog(@"%@",cost); }

i have used above code fetch uitableviewcell info not working showing first row info please tell have done wrong in above code how fetch info uibutton action method have stuck here long time please help me out.

thanks.

allocate uibutton cellforrowatindexpath in directly, other wise u result of lastly index on button action method

uibutton *button2 = [uibutton buttonwithtype:uibuttontypecustom]; button2.frame = cgrectmake(280,20, 40.0, 40.0); [button2 settitle:@"add" forstate:uicontrolstatenormal]; [button2 addtarget:self action:@selector(amethod2:) forcontrolevents:uicontroleventtouchupinside]; button2.tag=indexpath.row; [cell.contentview addsubview:button2];

your action method

-(void)amethod2:(uibutton*)sender { nsstring *item=[self.menuarray objectatindex:[sender tag]]; nslog(@"%@",item); nsstring *cost=[self.menuarray1 objectatindex:[sender tag]]; nslog(@"%@",cost); }

ios objective-c uitableview uibutton

No comments:

Post a Comment