i'm newbie. want create uitableview has 2 button , 3 label in each row. , want when load tableview, it'll check state of each button, set image them. after user click on other button in tabbar, uitableview reloaded , set image stats of them. how can that? in advance.
i tried below code image of marketbutton overlap price label works fine, not overlap:
- (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { static nsstring *cellidentifier = @"cell"; uitableviewcell *cell = (uitableviewcell *)[_tableview dequeuereusablecellwithidentifier:cellidentifier]; if (nil == cell) { cell = [[uitableviewcell alloc] initwithstyle:uitableviewcellstyledefault reuseidentifier:cellidentifier]; uilabel *pricelabel = [[uilabel alloc] initwithframe:cgrectmake(90, 0, 80, 30)]; pricelabel.backgroundcolor = [uicolor clearcolor]; pricelabel.font = [uifont fontwithname:@"helvetica" size:16]; pricelabel.font = [uifont boldsystemfontofsize:16]; pricelabel.textcolor = [uicolor darkgraycolor]; pricelabel.tag = 3000; //pricelabel.hidden = yes; pricelabel.textalignment = nstextalignmentright; [cell.contentview addsubview: pricelabel]; [pricelabel release]; } uibutton *marketbutton = [uibutton buttonwithtype:uibuttontypecustom]; [market addtarget:self action:@selector(marketpressed:) forcontrolevents:uicontroleventtouchdown]; [marketbutton settag:indexpath.row]; if([sellingarray count]>0) { nslog(@"sellingarray %@",sellingarray); if([[sellingarray objectatindex:indexpath.row] isequaltostring:@"0"]) // nothing { [marketbutton setselected:no]; [marketbutton setimage:[uiimage imagenamed:@"marketplace.png"] forstate:uicontrolstatenormal]; marketbutton.enabled = yes; } else if([[sellingarray objectatindex:indexpath.row] isequaltostring:@"2"]) // marketplace { [marketbutton setselected:yes]; [marketbutton setimage:[uiimage imagenamed:@"marketplaceselect.png"] forstate:uicontrolstatenormal]; marketbutton.enabled = yes; } } if([pricenewarray count]> 0) { uilabel *pricelbl = (uilabel*)[cell.contentview viewwithtag:3000]; pricelbl.text =[nsstring stringwithformat:@"$%@",[pricenewarray objectatindex:indexpath.row]]; if ([sellingarray count]>0) { if([[sellingarray objectatindex:indexpath.row] isequaltostring:@"2"]){ pricelbl.hidden = no; } else if([[sellingarray objectatindex:indexpath.row] isequaltostring:@"0"]){ pricelbl.hidden = yes; } } } return cell; }
i didnt see code adding marketbutton
in cell.
basically, when tableview reloaddata
called, call cellforrowatindexpath
, since using reusableidentifier
, return reused cell have label , button. if creating button instance again, overlap.
anyways, basic idea is, create labels , buttons inside cell==nil
block have created , assign tag them. outside cell==nil
block, control cell using tag , update control wish.
so logical flow of code this
if (nil == cell) { //create new cell instance reusable identifier cell = [[uitableviewcell alloc] initwithstyle:uitableviewcellstyledefault reuseidentifier:cellidentifier]; //create controls , assign tag uilabel *pricelabel = [[uilabel alloc] initwithframe:cgrectmake(90, 0, 80, 30)]; pricelabel.tag = 3000; [cell.contentview addsubview: pricelabel]; [pricelabel release]; uibutton *marketbutton = [uibutton buttonwithtype:uibuttontypecustom]; [market addtarget:self action:@selector(marketpressed:) forcontrolevents:uicontroleventtouchdown]; [marketbutton settag:3001]; [cell.contentview addsubview: marketbutton ]; } //in block outside, can controls cell , update it. uilabel *pricelbl = (uilabel*)[cell.contentview viewwithtag:3000]; uibutton*marketbtn = (uibutton*)[cell.contentview viewwithtag:3001]; //add code updating button based on status.
hope helps.