ios - change image of button in custom cell -
i want change image of button on custom cell,given code changing image on double click.i want change image on single click(check , uncheck)
- (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { static nsstring *simpletableidentifier = @"simpletablecell"; simpletablecell *cell = (simpletablecell *)[tableview dequeuereusablecellwithidentifier:simpletableidentifier]; if (cell == nil) { nsarray *nib = [[nsbundle mainbundle] loadnibnamed:@"simpletablecell" owner:self options:nil]; cell = [nib objectatindex:0]; } nsstring *localarraydata = [localarray objectatindex:indexpath.row]; cell.namelabel.text =localarraydata; cell.checkbutton.tag=indexpath.row; [cell.checkbutton setimage:[uiimage imagenamed:@"checku.png"] forstate:uicontrolstatenormal]; [cell.checkbutton addtarget:self action:@selector(checkbuttonaction:)forcontrolevents:uicontroleventtouchupinside]; return cell; } - (void)checkbuttonaction:(id)sender { nsinteger row = [sender tag]; nsindexpath *indexpath = [nsindexpath indexpathforrow:row insection:0]; simpletablecell *cell = [datatable cellforrowatindexpath:indexpath]; if(appdelegate.didselect) { [cell.checkbutton setimage:[uiimage imagenamed:@"checku.png"] forstate:uicontrolstatenormal]; appdelegate.didselect = false; } else { [cell.checkbutton setimage:[uiimage imagenamed:@"checks.png"] forstate:uicontrolstatenormal]; appdelegate.didselect = true; } }
you need this
-(void)selectuser:(id)sender { uibutton *button=(uibutton *) sender; nsindexpath *indexpath = [nsindexpath indexpathforrow:button.tag insection:0]; customtableviewcell *tappedcell = (customtableviewcell *)[yourtableview cellforrowatindexpath:indexpath]; if ([tappedcell.selectbutton.imageview.image isequal:[uiimage imagenamed:@"green.png"]]) { [tappedcell.selectbutton setimage:[uiimage imagenamed:@"grey.png"] forstate:uicontrolstatenormal]; } else { [tappedcell.selectbutton setimage:[uiimage imagenamed:@"green.png"] forstate:uicontrolstatenormal]; } }
in cellforrowatindexpath
method, set cell's indexpath button tag
cell.selectbutton.tag=indexpath.row;
and set selector button
[cell.selectbutton addtarget:self action:@selector(selectuser:) forcontrolevents:uicontroleventtouchupinside];
Comments
Post a Comment