ios - First UITableViewCell UIButton in search remains the same -
i have table loads 3 cells. first cell's label text "don" , lastly 2 cells' label texts "test".
i've noticed when search “don” first, uibutton tag 100 (correct tag #). next i'll search "test" , first cell's uibutton tag 100 (should 101) , sec cell's uibutton tag 102 (correct).
when rerun application , search "test" first, first cell's uibutton tag 101 (correct) , sec cell's uibutton tag 102 (correct). when search "don" afterwards, cell's uibutton tag 101 (should 100).
i'm not sure if it's overlapping on first cell or providing cell code below.
- (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { // register cell identifier custom cell nib static nsstring *cellidentifier = @"friendcell"; uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:cellidentifier]; // avatar settings uiimageview *imvavatar = [[uiimageview alloc] initwithframe:cgrectmake(3, 3, 45, 45)]; [imvavatar setimage:[uiimage imagenamed:@"btnavatar2.png"]]; imvavatar.layer.cornerradius = imvavatar.frame.size.height/2; imvavatar.layer.maskstobounds = yes; //imvavatar.layer.bordercolor = [uicolor colorwithred:59/255 green:59/255 blue:121/255 alpha:1].cgcolor; //imvavatar.layer.borderwidth = 1.0f; // befriend button settings uibutton *btnbefriend = [[uibutton alloc] initwithframe:cgrectmake(281, 14, 36, 22)]; [btnbefriend addtarget:self action:@selector(btnbefriendpressed:event:) forcontrolevents:uicontroleventtouchupinside]; // collect friend info if (tableview == self.searchdisplaycontroller.searchresultstableview) { friend = [searchresults objectatindex:indexpath.section]; } else { friend = [arrfriends objectatindex:indexpath.section]; } nsstring *user_id = (nsstring *)[friend objectatindex:0]; // user id nsstring *username = (nsstring *)[friend objectatindex:1]; // username nsstring *fname = (nsstring *)[friend objectatindex:2]; // first name nsstring *lname = (nsstring *)[friend objectatindex:3]; // lastly name nsstring *full_name = (nsstring *)[friend objectatindex:4]; // total name uiimage *picture = (uiimage *)[friend objectatindex:5]; // image (img) nsstring *type = (nsstring *)[friend objectatindex:6]; // type nsstring *arrindex = (nsstring *)[friend objectatindex:7]; // arrfriends index // configure cell if (!cell) { cell = [[uitableviewcell alloc] initwithstyle:uitableviewcellstyledefault reuseidentifier:cellidentifier]; // set width depending on device orientation cell.frame = cgrectmake(cell.frame.origin.x, cell.frame.origin.y, tableview.frame.size.width, cell.frame.size.height); // name settings uilabel *lblname = [[uilabel alloc] initwithframe:(cgrectmake(60, 3, 215, 45))]; [lblname setfont:[uifont systemfontofsize:14]]; // update name, status, picture, befriend button lblname.text = full_name; // total name imvavatar.image = picture; // image (img) if ([type isequaltostring:@""]) { [btnbefriend setimage:[uiimage imagenamed:@"btnbefriend.png"] forstate:uicontrolstatenormal]; } else { [btnbefriend setimage:[uiimage imagenamed:@"btnbefriended.png"] forstate:uicontrolstatenormal]; } // cell subviews imvavatar.tag = 1; lblname.tag = 2; btnbefriend.tag = [arrindex intvalue]+100; [cell.contentview addsubview:imvavatar]; [cell.contentview addsubview:lblname]; [cell.contentview addsubview:btnbefriend]; cell.clipstobounds = yes; } else { // create sure images, buttons , texts don't overlap // avatar uiimageview *imvavatar = (uiimageview *)[cell viewwithtag:1]; imvavatar.image = picture; // name uilabel *lblname = (uilabel *)[cell.contentview viewwithtag:2]; lblname.text = full_name; // befriendbutton uibutton *btnbefriend = (uibutton *)[cell.contentview viewwithtag:[arrindex intvalue]+100]; if ([type isequaltostring:@""]) { [btnbefriend setimage:[uiimage imagenamed:@"btnbefriend.png"] forstate:uicontrolstatenormal]; } else { [btnbefriend setimage:[uiimage imagenamed:@"btnbefriended.png"] forstate:uicontrolstatenormal]; } } homecoming cell; }
i assumed problem on lines:
friend = [searchresults objectatindex:indexpath.section]; and/or uibutton *btnbefriend = (uibutton *)[cell.contentview viewwithtag:[arrindex intvalue]+100];
but corect imageview , label texts appear, not exclusively sure what's causing problem.
help much appreciated. in advance.
you should create custom uitableviewcell avatr image , buttons , other necessary elements required , reuse custom cell. if u adding elements cell in - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath
then, cell reused not ui elements imvavatar
, btnbefriend
adding cell, these elements initialised . can cause memory issues well
ios objective-c uitableview uibutton
No comments:
Post a Comment