i using uitableview
preference screen, rows represent user selections.
maintaining selection using nsuserdefaults
. issue trying create special cell, building special color , using in following code:
- (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:@"uitableviewcell"]; if (!cell) { cell = [[uitableviewcell alloc] initwithstyle:uitableviewcellstyledefault reuseidentifier:@"uitableviewcell"]; } cell.textlabel.text = [myarray objectatindex:[indexpath row]]; uiview *selectioncolor = [[uiview alloc] init]; selectioncolor.backgroundcolor = [uicolor colorwithred:(200/255.0) green:(200/255.0) blue:(200/255.0) alpha:0.6]; cell.selectedbackgroundview = selectioncolor; cell.textlabel.textcolor = [uicolor graycolor]; return cell; }
based on this, when user click occurs, displays grayish color building in event, want. when user deselects it, greyish background color gone.
now need add nsuserdefaults
, check whether cell saved. events becomes now:
- (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:@"uitableviewcell"]; if (!cell) { cell = [[uitableviewcell alloc] initwithstyle:uitableviewcellstyledefault reuseidentifier:@"uitableviewcell"]; } cell.textlabel.text = [myarray objectatindex:[indexpath row]]; uiview *selectioncolor = [[uiview alloc] init]; selectioncolor.backgroundcolor = [uicolor colorwithred:(200/255.0) green:(200/255.0) blue:(200/255.0) alpha:0.6]; cell.selectedbackgroundview = selectioncolor; cell.textlabel.textcolor = [uicolor graycolor]; if([[nsuserdefaults standarduserdefaults] valueforkey:selectedoption]) { //here, how can make cell grayish in background? //if cell.selected = true// cell shows black background color } return cell; }
if call cell.selected = true
cell has black background , not looking for. @ loss should perform customization.
update : following rdelmar's answer updated cellforrowatindexpath , added customisation code
if (myarray[indexpath.row][selectionstate] == yes){ uiview *selectioncolor = [[uiview alloc] init]; selectioncolor.backgroundcolor = [uicolor colorwithred:(200/255.0) green:(200/255.0) blue:(200/255.0) alpha:0.6]; cell.selectedbackgroundview = selectioncolor; cell.textlabel.textcolor = [uicolor graycolor]; }else{ cell.selectedbackgroundview = nil; cell.textlabel.textcolor = [uicolor blackcolor];
}
still didn;'r resolve issue, meaning pass verification l(selectionstate == yes, , hit code change color of background view, when displaying still unchanged.
i found method willdisplaycell, , moved code customaisation there, still didn';t work !!
- (uitableviewcell *)tableview:(uitableview *)tableview willdisplaycell:(uitableviewcell *)cell forrowatindexpath:(nsindexpath *)indexpath { if (myarray[indexpath.row][selectionstate] == yes){ uiview *selectioncolor = [[uiview alloc] init]; selectioncolor.backgroundcolor = [uicolor colorwithred:(200/255.0) green:(200/255.0) blue:(200/255.0) alpha:0.6]; cell.selectedbackgroundview = selectioncolor; } else{ cell.selectedbackgroundview = nil; cell.textlabel.textcolor = [uicolor blackcolor]; } return cell; }
am supposed intercept event or method?
i'm not sure want in user defaults -- if do, you'll have somehow keep track of index in array (that supplies data cells) selected cell corresponds to.
i change structure of myarray array of dictionaries 2 keys, 1 data, , 1 selection state of cell. update value of cell state in didselectrowatindexpath , diddeselectrowatindexpath. then, in cellforrowatindexpath, check value of cell state key (lets call selectionstate):
if (myarray[indexpath.row][selectionstate] == yes){ uiview *selectioncolor = [[uiview alloc] init]; selectioncolor.backgroundcolor = [uicolor colorwithred:(200/255.0) green:(200/255.0) blue:(200/255.0) alpha:0.6]; cell.selectedbackgroundview = selectioncolor; cell.textlabel.textcolor = [uicolor graycolor]; }else{ cell.selectedbackgroundview = nil; cell.textlabel.textcolor = [uicolor blackcolor]; }
of course, you'll have save array disk make cell state value persistent.
Comments
Post a Comment