ios - UIButton setImage in UITableViewCell crashes app -
as of right now, table loads 3 cells. 2 of cells' labels include string "test". when search "test" first time, expected cells load , right images, label texts , button backgrounds displayed. nil overlaps. far there.
however, app crashes when cancel search , go , search "test" 1 time again (as come in lastly character "t").
i error:
-[uiimageview setimage:forstate:]: unrecognized selector sent instance 0x9128140 2014-06-19 14:12:19.982 appname[5171:60b] *** terminating app due uncaught exception 'nsinvalidargumentexception', reason: '-[uiimageview setimage:forstate:]: unrecognized selector sent instance 0x9128140'
i made sure instance uibutton , not uiimageview in cell. read on other answers add together 100 button tags, nil changed. way, button tags right when search. (i checked alert every button click)
below code cells.
- (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; // befriend button settings uibutton *btnbefriend = [[uibutton alloc] initwithframe:cgrectmake(281, 14, 36, 22)]; [btnbefriend addtarget:self action:@selector(btnbefriendpressed:event:) forcontrolevents:uicontroleventtouchupinside]; if (tableview == self.searchdisplaycontroller.searchresultstableview) { friend = [searchresults objectatindex:indexpath.section]; } else { friend = [arrfriends objectatindex:indexpath.section]; } // collect friend info 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]; [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]]; if ([type isequaltostring:@""]) { [btnbefriend setimage:[uiimage imagenamed:@"btnbefriend.png"] forstate:uicontrolstatenormal]; } else { [btnbefriend setimage:[uiimage imagenamed:@"btnbefriended.png"] forstate:uicontrolstatenormal]; } } homecoming cell; }
help much appreciated. in advance!
i believe problem on line:
uibutton *btnbefriend = (uibutton *)[cell.contentview viewwithtag:[arrindex intvalue]];
you're expecting uibutton
actual view beingness returned uiimageview
. it's possible [arrindex intvalue]
has same tag image view that's subview of cell's content view.
ios objective-c uitableview uibutton
No comments:
Post a Comment