ios - How to select all uitableview cells on button click -
i making app in using buttons in form of checkboxes in uitableview cell.i changing images on checkbox select , unselect state. want when click on button cells of table view select , image of checkboxes of cells changed? how can done below sample code
- (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section { return self.lblarray.count; } - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { static nsstring *tableviewidentifier = @"cell"; tablecelltableviewcell *cell= [self.tblvie dequeuereusablecellwithidentifier:tableviewidentifier]; if(cell==nil) { cell = [[tablecelltableviewcell alloc] initwithstyle:uitableviewcellstylesubtitle reuseidentifier:tableviewidentifier]; } cell.lblimage.layer.cornerradius=3.0f; cell.lblimage.layer.borderwidth=1.0f; cell.backgroundcolor=[uicolor colorwithred:(245/255.0) green:(245/255.0) blue:(245/255.0) alpha:1]; if ([self.checkimagearray containsobject:[self.lblarray objectatindex:indexpath.row]]) { [cell.buttoncheckbox setimage:[uiimage imagenamed:@"check_box_ok.png"] forstate:uicontrolstatenormal]; } else { [cell.buttoncheckbox setimage:[uiimage imagenamed:@"uncheck.png"] forstate:uicontrolstatenormal]; } cell.buttoncheckbox.tag=indexpath.row; [cell.buttoncheckbox addtarget:self action:@selector(checkbutton:) forcontrolevents:uicontroleventtouchupinside]; cell.lbltext.text=[self.lblarray objectatindex:indexpath.row]; return cell; } -(ibaction)checkbutton:(uibutton *)sender { cgpoint buttonposition = [sender convertpoint:cgpointzero toview:self.tblvie]; nsindexpath *indexpath = [self.tblvie indexpathforrowatpoint:buttonposition]; if ([self.checkimagearray containsobject:[self.lblarray objectatindex:indexpath.row]]) { [self.checkimagearray removeobject:[self.lblarray objectatindex:indexpath.row]]; self.titlelabel.text=@""; } else { [self.checkimagearray addobject:[self.lblarray objectatindex:indexpath.row]]; self.titlelabel.text=[self.lblarray objectatindex:sender.tag]; } [self.tblvie reloadrowsatindexpaths:[nsarray arraywithobject:indexpath] withrowanimation: uitableviewrowanimationfade]; }
and here button in click cells of tableview select , images of cells changed not working
- (ibaction)selectbtns:(id)sender { static nsstring *tableviewidentifier = @"cell"; tablecelltableviewcell *cell= [self.tblvie dequeuereusablecellwithidentifier:tableviewidentifier]; [cell.buttoncheckbox setimage:[uiimage imagenamed:@"check_box_ok.png"] forstate:uicontrolstatenormal]; }
nsmutablearray *totalcheckmarkarray; //add on nsmutablearray in globally - (void)viewdidload { totalcheckmarkarray =[[nsmutablearray alloc]init]; (int i=0; i<[self.lblarray count]; i++) // number of rows count { [self.totalcheckmarkarray addobject:@"no"]; } } -(uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { static nsstring *tableviewidentifier = @"cell"; tablecelltableviewcell *cell= [self.tblvie dequeuereusablecellwithidentifier:tableviewidentifier]; if(cell==nil) { cell = [[tablecelltableviewcell alloc] initwithstyle:uitableviewcellstylesubtitle reuseidentifier:tableviewidentifier]; } cell.lblimage.layer.cornerradius=3.0f; cell.lblimage.layer.borderwidth=1.0f; if(![[self.totalcheckmarkarray objectatindex:indexpath.row] isequaltostring:@"no"]) { [cell.buttoncheckbox setimage:[uiimage imagenamed:@"check_box_ok.png"] forstate:uicontrolstatenormal]; } else if ([self.checkimagearray containsobject:[self.lblarray objectatindex:indexpath.row]]) { [cell.buttoncheckbox setimage:[uiimage imagenamed:@"check_box_ok.png"] forstate:uicontrolstatenormal]; } else { [cell.buttoncheckbox setimage:[uiimage imagenamed:@"uncheck.png"] forstate:uicontrolstatenormal]; } cell.buttoncheckbox.tag=indexpath.row; [cell.buttoncheckbox addtarget:self action:@selector(checkbutton:) forcontrolevents:uicontroleventtouchupinside]; cell.lbltext.text=[self.lblarray objectatindex:indexpath.row]; return cell; }
selectall checkbox
- (ibaction)selectbtns:(id)sender { (int i=0; i<[self.lblarray count]; i++) { [totalcheckmarkarray replaceobjectatindex:i withobject:@"yes"]; } [self.tblvie reloaddata]; }
deselectall checkbox
-(ibaction)deselectall_btnclick:(id)sender { (int i=0; i<[self.lblarray count]; i++) { [totalcheckmarkarray replaceobjectatindex:i withobject:@"no"]; } [self.tblvie reloaddata]; }
Comments
Post a Comment