ios - Adding rows dynamically in UITableView and setting scrollToRowAtIndexPath -
dynamically adding rows on click of button @ index path n-3. in table view default there total 6 rows 4 contains textfields , other 2 contains button(add more , save). adding buttons @ location above last 2 rows.
-(void)addmorebutton:(uibutton *)sender { rowcount=rowcount+1; int section = 0; long row = rowcount; nsindexpath* path = [nsindexpath indexpathforrow:row-3 insection:section]; [(uitableview *)[self.view viewwithtag:ktagtableview_detailsedit] beginupdates]; [(uitableview *)[self.view viewwithtag:ktagtableview_detailsedit] insertrowsatindexpaths:[nsarray arraywithobjects:path, nil] withrowanimation:uitableviewrowanimationleft]; [(uitableview *)[self.view viewwithtag:ktagtableview_detailsedit] endupdates]; nsindexpath* top = [nsindexpath indexpathforrow:rowcount-1 insection:0]; [(uitableview*)[self.view viewwithtag:ktagtableview_detailsedit] scrolltorowatindexpath:top atscrollposition:uitableviewscrollpositionbottom animated:yes]; }
the problem arises when rowcount reaches more 8 , scrolltorowatindexpath method called cellforrowatindexpath method called twice. once called insertrowatindex passes index n-3 , second time when scrolltorowatindexpath called passes index n-1 runs condition when
if (indexpath.row == rowcount-1) // index last row contains save button , condition match instead of adding textfields starts adding save button. if (indexpath.row == rowcount-2) // add more button. if (indexpath.row < rowcount-2) // adding textfields.
please guide above. , feel free ask if query. stuck in last 2 days.
update: cellforrowatindexpath method.
- (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { nsstring *cellidentifier; cellidentifier = [nsstring stringwithformat:@"s%1dr%1d",indexpath.section,indexpath.row]; uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier: cellidentifier]; cellidentifier = [nsstring stringwithformat:@"s%1dr%1d",indexpath.section,indexpath.row]; uitextfield *textfield_socialprofile; uibutton *button_addmore, *button_addtomycon; uiimageview *imageview_add; if(cell == nil) { cell = [[uitableviewcell alloc]initwithstyle:uitableviewcellstyledefault reuseidentifier:cellidentifier]; [cell setselectionstyle:uitableviewcellselectionstylenone]; [cell setbackgroundcolor:[uicolor clearcolor]]; linelable=[[uilabel alloc]initwithframe:cgrectmake(0, cell.contentview.frame.size.height+9, cell.contentview.frame.size.width, 0.5)]; [cell.contentview addsubview:linelable]; [linelable setbackgroundcolor:[uicolor colorwithred:(152.0f/255.0f) green:(152.0f/255.0f) blue:(152.0f/255.0f) alpha:0.2f]]; if (indexpath.row < rowcount-2) { textfield_socialprofile = [[uitextfield alloc]init]; textfield_socialprofile.tag=indexpath.row+501; nslog(@"textfield_socialprofile.tag %ld",(long)textfield_socialprofile.tag); textfield_socialprofile.frame = cgrectmake(70, (tableview.rowheight - 20)/2, 230, 30); textfield_socialprofile.delegate = self; textfield_socialprofile.borderstyle = uitextborderstyleroundedrect; switch (indexpath.row) { case 0: textfield_socialprofile.attributedplaceholder = [[nsattributedstring alloc] initwithstring:ktaginstagram attributes:@{nsforegroundcolorattributename: [uicolor lightgraycolor]}]; break; case 1: textfield_socialprofile.attributedplaceholder = [[nsattributedstring alloc] initwithstring:ktagfacebook attributes:@{nsforegroundcolorattributename: [uicolor lightgraycolor]}]; break; case 2: textfield_socialprofile.attributedplaceholder = [[nsattributedstring alloc] initwithstring:ktagtwitter attributes:@{nsforegroundcolorattributename: [uicolor lightgraycolor]}]; break; case 3: textfield_socialprofile.attributedplaceholder = [[nsattributedstring alloc] initwithstring:@"linkedin" attributes:@{nsforegroundcolorattributename: [uicolor lightgraycolor]}]; break; default: textfield_socialprofile.attributedplaceholder = [[nsattributedstring alloc] initwithstring:@"social networking" attributes:@{nsforegroundcolorattributename: [uicolor lightgraycolor]}]; } textfield_socialprofile.textcolor = [uicolor colorwithred:(152.0f/255.0f) green:(152.0f/255.0f) blue:(152.0f/255.0f) alpha:1.0f]; textfield_socialprofile.layer.cornerradius = 5.0; textfield_socialprofile.clipstobounds = yes; [textfield_socialprofile.layer setbordercolor:[uicolor lightgraycolor].cgcolor]; [textfield_socialprofile.layer setborderwidth:0.5]; if (inteditsave==1) { [textfield_socialprofile setuserinteractionenabled:no]; } else { [textfield_socialprofile setuserinteractionenabled:yes]; } imageview_add = [[uiimageview alloc]init]; switch (indexpath.row) { case 0: imageview_add.image = [uiimage imagenamed:@"instagramicon.png"]; textfield_socialprofile.text=@"www.instagram.com/"; if (datasourcearray.count > indexpath.row) { textfield_socialprofile.text = [[datasourcearray objectatindex:indexpath.row] objectforkey:@"socialprofile"]; if ([[nsstring stringwithformat:@"%@",[[datasourcearray objectatindex:indexpath.row] objectforkey:@"socialprofile"]] length] != 0) { textfield_socialprofile.text=[[datasourcearray objectatindex:indexpath.row] objectforkey:@"socialprofile"]; if([textfield_socialprofile.text isequaltostring:@""]) { textfield_socialprofile.text=[nsstring stringwithformat:@"www.instagram.com/%@",[[datasourcearray objectatindex:indexpath.row] objectforkey:@"socialprofile"]]; } }else{ textfield_socialprofile.text=@"www.instagram.com/"; } } break; case 1: imageview_add.image = [uiimage imagenamed:@"facebokicon.png"]; textfield_socialprofile.text=@"www.facebook.com/"; if (datasourcearray.count > indexpath.row) { textfield_socialprofile.text = [[datasourcearray objectatindex:indexpath.row] objectforkey:@"socialprofile"]; if ([[nsstring stringwithformat:@"%@",[[datasourcearray objectatindex:indexpath.row] objectforkey:@"socialprofile"]] length] != 0) { textfield_socialprofile.text=[[datasourcearray objectatindex:indexpath.row] objectforkey:@"socialprofile"]; if([textfield_socialprofile.text isequaltostring:@""]) { textfield_socialprofile.text=[nsstring stringwithformat:@"www.facebook.com/%@",[[datasourcearray objectatindex:indexpath.row] objectforkey:@"socialprofile"]]; } }else{ textfield_socialprofile.text=@"www.facebook.com/"; } } break; case 2: imageview_add.image = [uiimage imagenamed:@"twittericon.png"]; textfield_socialprofile.text=@"www.twitter.com/"; if (datasourcearray.count > indexpath.row) { textfield_socialprofile.text = [[datasourcearray objectatindex:indexpath.row] objectforkey:@"socialprofile"]; if ([[nsstring stringwithformat:@"%@",[[datasourcearray objectatindex:indexpath.row] objectforkey:@"socialprofile"]] length] != 0) { textfield_socialprofile.text=[[datasourcearray objectatindex:indexpath.row] objectforkey:@"socialprofile"]; if([textfield_socialprofile.text isequaltostring:@""]) { textfield_socialprofile.text=[nsstring stringwithformat:@"www.twitter.com/%@",[[datasourcearray objectatindex:indexpath.row] objectforkey:@"socialprofile"]]; } }else{ textfield_socialprofile.text=@"www.twitter.com/"; } } break; case 3: imageview_add.image = [uiimage imagenamed:@"linkedinicon.png"]; textfield_socialprofile.text=@"www.linkedin.com/"; if (datasourcearray.count > indexpath.row) { textfield_socialprofile.text = [[datasourcearray objectatindex:indexpath.row] objectforkey:@"socialprofile"]; if ([[nsstring stringwithformat:@"%@",[[datasourcearray objectatindex:indexpath.row] objectforkey:@"socialprofile"]] length] != 0) { textfield_socialprofile.text=[[datasourcearray objectatindex:indexpath.row] objectforkey:@"socialprofile"]; if([textfield_socialprofile.text isequaltostring:@""]) { textfield_socialprofile.text=[nsstring stringwithformat:@"www.linkedin.com/%@",[[datasourcearray objectatindex:indexpath.row] objectforkey:@"socialprofile"]]; } }else{ textfield_socialprofile.text=@"www.linkedin.com/"; } } break; default: imageview_add.image = [uiimage imagenamed:@"myconicon.png"]; if (datasourcearray.count > indexpath.row) { textfield_socialprofile.text = [[datasourcearray objectatindex:indexpath.row] objectforkey:@"socialprofile"];} break; } nsstring *socialname= textfield_socialprofile.text; switch (indexpath.row) { case 0: if ([socialname rangeofstring:@"www.instagram.com/"].location!=nsnotfound) { imageview_add.image = [uiimage imagenamed:@"instagramicon.png"]; } else if ([socialname rangeofstring:@"www.linkedin.com/"].location!=nsnotfound) { imageview_add.image = [uiimage imagenamed:@"linkedinicon.png"]; } else if ([socialname rangeofstring:@"www.facebook.com/"].location!=nsnotfound) { imageview_add.image = [uiimage imagenamed:@"facebokicon.png"]; } else if ([socialname rangeofstring:@"www.twitter.com/"].location!=nsnotfound) { imageview_add.image = [uiimage imagenamed:@"twittericon.png"]; } else { imageview_add.image = [uiimage imagenamed:@"myconicon.png"]; } break; case 1: if ([socialname rangeofstring:@"www.instagram.com/"].location!=nsnotfound) { imageview_add.image = [uiimage imagenamed:@"instagramicon.png"]; } else if ([socialname rangeofstring:@"www.linkedin.com/"].location!=nsnotfound) { imageview_add.image = [uiimage imagenamed:@"linkedinicon.png"]; } else if ([socialname rangeofstring:@"www.facebook.com/"].location!=nsnotfound) { imageview_add.image = [uiimage imagenamed:@"facebokicon.png"]; } else if ([socialname rangeofstring:@"www.twitter.com/"].location!=nsnotfound) { imageview_add.image = [uiimage imagenamed:@"twittericon.png"]; } else { imageview_add.image = [uiimage imagenamed:@"myconicon.png"]; } break; case 2: if ([socialname rangeofstring:@"www.instagram.com/"].location!=nsnotfound) { imageview_add.image = [uiimage imagenamed:@"instagramicon.png"]; } else if ([socialname rangeofstring:@"www.linkedin.com/"].location!=nsnotfound) { imageview_add.image = [uiimage imagenamed:@"linkedinicon.png"]; } else if ([socialname rangeofstring:@"www.facebook.com/"].location!=nsnotfound) { imageview_add.image = [uiimage imagenamed:@"facebokicon.png"]; } else if ([socialname rangeofstring:@"www.twitter.com/"].location!=nsnotfound) { imageview_add.image = [uiimage imagenamed:@"twittericon.png"]; } else { imageview_add.image = [uiimage imagenamed:@"myconicon.png"]; } break; case 3: if ([socialname rangeofstring:@"www.instagram.com/"].location!=nsnotfound) { imageview_add.image = [uiimage imagenamed:@"instagramicon.png"]; } else if ([socialname rangeofstring:@"www.linkedin.com/"].location!=nsnotfound) { imageview_add.image = [uiimage imagenamed:@"linkedinicon.png"]; } else if ([socialname rangeofstring:@"www.facebook.com/"].location!=nsnotfound) { imageview_add.image = [uiimage imagenamed:@"facebokicon.png"]; } else if ([socialname rangeofstring:@"www.twitter.com/"].location!=nsnotfound) { imageview_add.image = [uiimage imagenamed:@"twittericon.png"]; } else { imageview_add.image = [uiimage imagenamed:@"myconicon.png"]; } break; default: if ([socialname rangeofstring:@"www.instagram.com/"].location!=nsnotfound) { imageview_add.image = [uiimage imagenamed:@"instagramicon.png"]; } else if ([socialname rangeofstring:@"www.linkedin.com/"].location!=nsnotfound) { imageview_add.image = [uiimage imagenamed:@"linkedinicon.png"]; } else if ([socialname rangeofstring:@"www.facebook.com/"].location!=nsnotfound) { imageview_add.image = [uiimage imagenamed:@"facebokicon.png"]; } else if ([socialname rangeofstring:@"www.twitter.com/"].location!=nsnotfound) { imageview_add.image = [uiimage imagenamed:@"twittericon.png"]; } else { imageview_add.image = [uiimage imagenamed:@"myconicon.png"]; } break; } imageview_add.frame = cgrectmake(30, (tableview.rowheight - 8)/2, 15, 15); textfield_socialprofile.font = [uifont fontwithname:@"arial" size:15.0]; [cell addsubview:imageview_add]; imageview_add = nil; [cell addsubview:textfield_socialprofile]; //textfield_socialprofile = nil; } else if(indexpath.row == rowcount-2)//add more button { // nslog(@"textf: %@",textf.text); nslog(@"addmore: %ld",indexpath.row); nslog(@"addmore: %d",rowcount); button_addmore = [[uibutton alloc]init]; [button_addmore settitle:ktagadd_more forstate:uicontrolstatenormal]; [button_addmore titlelabel].font = [uifont fontwithname:@"arial" size:15.0]; [button_addmore settitlecolor:[uicolor whitecolor] forstate:uicontrolstatenormal]; [button_addmore addtarget:self action:@selector(addmorebutton:) forcontrolevents:uicontroleventtouchupinside]; button_addmore.tag = ktagbutton_addmore; button_addmore.frame = cgrectmake(90, (cell.frame.size.height-20)/2, 140, 30); button_addmore.center=cgpointmake(90+70, 22); [button_addmore setbackgroundimage:[uiimage imagenamed:@"addmore_button.png"] forstate:uicontrolstatenormal]; [cell addsubview:button_addmore]; linelable.hidden=no; if(inteditsave == 1)//editing complete { button_addmore.enabled=no; } else { button_addmore.enabled=yes; } } else if(indexpath.row == rowcount-1)//save fields button { nslog(@"save: %ld",indexpath.row); nslog(@"save: %d",rowcount); button_addtomycon = [[uibutton alloc]init]; [button_addtomycon settitle:ktagadd_to_mycon forstate:uicontrolstatenormal]; [button_addtomycon titlelabel].font = [uifont fontwithname:@"arial" size:15.0]; [button_addtomycon settitlecolor:[uicolor whitecolor] forstate:uicontrolstatenormal]; [button_addtomycon addtarget:self action:@selector(save_fields:) forcontrolevents:uicontroleventtouchupinside]; button_addtomycon.tag = ktagbutton_addtomycon; button_addtomycon.frame = cgrectmake(90, (cell.frame.size.height-20)/2, 140, 30); button_addtomycon.center=cgpointmake(90+70, 22); [button_addtomycon setbackgroundcolor:[uicolor colorwithred:(234.0f/255.0f) green:(134.0f/255.0f) blue:(59.0f/255.0f) alpha:1.0]]; button_addtomycon.layer.cornerradius=4; [cell addsubview:button_addtomycon]; linelable.hidden=yes; } } return cell; }
the trouble if set dequereusable property nil, problem solves , current data starts vanishing when scrolled.
this behaviour of tableview datasource , delegate methods. cellforrowatindexpath
method called when scrolltorowatindexpath
because when table scroll show cell not visible on screen method performs reuse cell, cellforrowatindexpath
load content new visible cells.
it default , correct behaviour, can not pass it.
now need manage app requirement it. don't think behaviour makes loop hole requirement.
because cellforrowatindexpath
method called several times , can not limit constraints here. still leave comment if think expected behaviour not achieved.
edit:
while creating sample demo you, figure out 1 point - may sort out problem quickly.
in below line animation not working proper:
[(uitableview *)[self.view viewwithtag:ktagtableview_detailsedit] insertrowsatindexpaths:[nsarray arraywithobjects:path, nil] withrowanimation:uitableviewrowanimationleft];
change row animation uitableviewrowanimationleft
uitableviewrowanimationautomatic
, see below updated line:
[(uitableview *)[self.view viewwithtag:ktagtableview_detailsedit] insertrowsatindexpaths:[nsarray arraywithobjects:indexpath, nil] withrowanimation:uitableviewrowanimationautomatic];
still if not solve problem have sample demo here...
for sake of simplicity have limit control , focus on functionality.
Comments
Post a Comment