ios - selector for multiple uibuttons in uitableviewcell -
i'm adding same selector multiple uibuttons
part of uitableviewcell
, works first one. missing something?
here's code:
- (void)tableview:(uitableview *)tableview willdisplaycell:(uitableviewcell *)cell forrowatindexpath:(nsindexpath *)indexpath{ nsdictionary *d = [tabledata objectatindex:indexpath.row]; nsarray *answers = [d objectforkey:@"answers"];//array of { id = 35; text = "\u03c4\u03a\u03c0\u03bf\u03c4\u03b1"; } uilabel *startdate = (uilabel *)[cell viewwithtag:100]; long long startdateepoch = [[d objectforkey:@"startdate"] longlongvalue]; nsdate *sdate = [nsdate datewithtimeintervalsince1970:startdateepoch/1000]; nsdateformatter *startdateformatter = [[nsdateformatter alloc] init]; [startdateformatter setdateformat:@"dd/mm/yyyy"]; startdate.text = [startdateformatter stringfromdate:sdate]; ((uilabel *)[cell viewwithtag:101]).text = [d objectforkey:@"subject"]; nsarray *tags = [d objectforkey:@"tags"]; ((uilabel *)[cell viewwithtag:102]).text = [tags componentsjoinedbystring:@" "]; nsstring *imageurl = [d objectforkey:@"media"]; uiimageview *iv = (uiimageview *)[cell viewwithtag:103]; iv.contentmode = uiviewcontentmodescaleaspectfit; [iv sd_setimagewithurl:[nsurl urlwithstring:imageurl] placeholderimage:[uiimage imagenamed:@"placeholder.png"]]; uiview *buttonscontainer = (uiview *)[cell viewwithtag:104]; for(uiview *vv in buttonscontainer.subviews){ [vv removefromsuperview]; } int index = 0; nslog(@"buttonscontainer children: %d", buttonscontainer.subviews.count); for(nsdictionary *answer in answers){ uiview *v = [[uiview alloc] initwithframe:cgrectmake(0, index * 40 + (index+1)*5, [uiscreen mainscreen].bounds.size.width, 40)]; v.backgroundcolor = [uicolor whitecolor]; cgrect labelframe = v.frame; labelframe.origin.y = 0; uilabel *l = [[uilabel alloc] initwithframe:labelframe]; l.text = (nsstring *)[answer objectforkey:@"text"]; l.textalignment = nstextalignmentcenter; uiview *bottomline = [[uiview alloc] initwithframe:cgrectmake(0, v.frame.size.height - 1, v.frame.size.width, 1)]; bottomline.backgroundcolor = [uicolor colorwithred:238.0/255.0 green:205.0/255.0 blue:103.0/255.0 alpha:1.0]; uibutton *b = [[uibutton alloc] initwithframe:v.frame]; [b addtarget:self action:@selector(answered:) forcontrolevents:uicontroleventtouchupinside]; [v addsubview:l]; [v addsubview:bottomline]; [v addsubview:b]; [buttonscontainer addsubview:v]; index++; } }
so example if 4 buttons added, selector called when click on first one. when click on other 3 nothing happens.
edit: adding answered:
code:
- (void)answered:(uibutton *)sender { nslog(@"@answered"); }
ok, found it. many cudos tomswift since point gave me idea place borders on views in order debug frames size , origin.
notice give buttons same frame superviews, wrong since should not have same origin. buttons' origin should 0,0 since have same frame size superviews.
lessons learn noobs me:
- when button not work, place borders in order make sure frame within superview's bounds.
- do not assign same frame on view , subview. place outside of superview's bounds in 92% of cases brings undesirable behaviour.
Comments
Post a Comment