ios - UITableView Cell colour changes on scrolling query -
in application taking question in nsmutablearray , displaying on tableview row. have taken 2 buttons under each question(tableview row button). pressing yes , no button , tablerow colour changes green, yellow respectively.
the problem after pressing button in particular row row turns green. after scrolling unwanted cells turning green/yellow.
could please check below code , suggest
- (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { contentview.hidden = yes; if( tableview == mychklist) { static nsuinteger const kquestlabeltag = 1; static nsuinteger const kyesbuttontag = 2; static nsuinteger const knobuttontag = 3; uilabel *questlabel = nil; uibutton *yesbutton; uibutton *nobutton; static nsstring *cellidentifier = @"cell"; uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:cellidentifier]; if (cell == nil) { cell = [[uitableviewcell alloc] initwithstyle:uitableviewcellstyledefault reuseidentifier:cellidentifier]; cell.selectionstyle = uitableviewcellselectionstylenone; //cell bg //self.mychklist.backgroundcolor = [uicolor clearcolor]; if (ui_user_interface_idiom() == uiuserinterfaceidiompad) { questlabel = [[uilabel alloc] initwithframe:cgrectmake(5,5,768,0)]; //some logic code [cell.contentview addsubview:questlabel]; nsstring *titlestring = [buildingquest objectatindex:indexpath.row] ; cgsize titlesize = [titlestring sizewithfont:[uifont boldsystemfontofsize:18] constrainedtosize:cgsizemake(670, maxfloat) linebreakmode:nslinebreakbywordwrapping]; uibutton *yesbutton = [uibutton buttonwithtype:uibuttontypecustom]; //some logic button code [cell.contentview addsubview:yesbutton]; uibutton *nobutton = [uibutton buttonwithtype:uibuttontypecustom]; //some logic button code [cell.contentview addsubview:nobutton]; } else { questlabel = (uilabel *)[cell.contentview viewwithtag:kquestlabeltag]; yesbutton = (uibutton *)[cell.contentview viewwithtag:kyesbuttontag]; nobutton = (uibutton *)[cell.contentview viewwithtag:knobuttontag]; } questlabel.text = [buildingquest objectatindex:indexpath.row]; questlabel.numberoflines = 0; nsstring *titlestring = [buildingquest objectatindex:indexpath.row] ; if (ui_user_interface_idiom() == uiuserinterfaceidiompad) { cgsize titlesize = [titlestring sizewithfont:[uifont boldsystemfontofsize:18] constrainedtosize:cgsizemake(670, maxfloat) linebreakmode:nslinebreakbywordwrapping]; cgrect rect = questlabel.frame; rect.size.height = titlesize.height+10; questlabel.frame = rect; yesbutton.frame = cgrectmake(10,titlesize.height+15,60,30); nobutton.frame = cgrectmake(95,titlesize.height+15,60,30); } if([self getcheckedforindex:indexpath.row]==yes) { cell.backgroundcolor = [uicolor greencolor]; cam_button.hidden = no; } else { cell.accessorytype = uitableviewcellaccessorynone; } if([self getcheckedforyellow_index:indexpath.row]==yes) { cell.backgroundcolor = [uicolor yellowcolor]; } else { cell.accessorytype = uitableviewcellaccessorynone; } return cell; } } - (void)tableview:(uitableview *)tableview didselectrowatindexpath:(nsindexpath *)indexpath { uitableviewcell *cell = [tableview cellforrowatindexpath:indexpath]; if([self getcheckedforindex:indexpath.row]==yes) { cell.backgroundcolor = [uicolor greencolor]; cam_button.hidden = no; } if([self getcheckedforyellow_index:indexpath.row]==yes) { cell.backgroundcolor = [uicolor yellowcolor]; } if ([self.arraycheckunchek containsobject:indexpath]) { cell.backgroundcolor = [uicolor greencolor]; cam_button.hidden = no; } if ([arraycheckunchek2 containsobject:indexpath]) { cell.backgroundcolor = [uicolor yellowcolor]; } } - (void)tableview: (uitableview*)tableview willdisplaycell: (uitableviewcell*)cell forrowatindexpath: (nsindexpath*)indexpath { uitableviewcell *cell = [mychklist cellforrowatindexpath:indexpath]; //same code didselectrowatindexpath method } -(void)noaction:(id)sender event: (id)event { _cellbuttonpress = yes; checkuncheck = yes; nsset *touches = [event alltouches]; uitouch *touch = [touches anyobject]; cgpoint currenttouchposition = [touch locationinview:self.mychklist]; nsindexpath *indexpath = [self.mychklist indexpathforrowatpoint: currenttouchposition]; uibutton *button = (uibutton *)sender; uitableviewcell *cell = (uitableviewcell *)button.superview.superview; cell.backgroundcolor = [uicolor yellowcolor]; [self checkedcellatyellow_index:indexpath.row]; [arraycheckunchek2 addobject:indexpath]; }
you need set background color of newly allocated cells. if don't that, when cells had turned green reused, stay green.
if (cell == nil) { cell.backgroundcolor = [uicolor clearcolor]; ...
Comments
Post a Comment