How to set selection to single for all sections in the tableview in ios, objective c -


i have table view 5 sections , have set tableview selection multiple.each section has different rows.what want set , user can select 1 cell each section(in table user can select number of cells). ex: 5 cells 5 sections.

should impossible select more 1 cell each section.if user select cell same cell, selected cell should deselect.how can this.this sample implementation of didselectrowatindexpath.

- (void)tableview:(uitableview *)tableview didselectrowatindexpath:(nsindexpath *)indexpath {       hoteldetalcellonetableviewcell *cellone = (hoteldetalcellonetableviewcell *)[self.detailtableview cellforrowatindexpath:indexpath];     hoteldetailcelltwotableviewcell *celltwo = (hoteldetailcelltwotableviewcell *)[self.detailtableview cellforrowatindexpath:indexpath];      //i have implement 2 sections test.     if(indexpath.section == 0)     {         hoteldetailsone *secone = [roomonearray objectatindex:indexpath.row];        hoteldetailsforbooking *book = [hoteldetailsforbooking new];         if([secone.offerallow isequaltostring:@"true"])         {             celltwo.selectedsignlabel.hidden = no;          }         else         {              cellone.selectedsignlabelone.hidden = no;          } //        [self.detailtableview reloaddata];       nslog(@"price room 1 : %@", secone.itempricetext);      }     else     {         hoteldetailsone *sectwo = [roomtwoarray objectatindex:indexpath.row];         hoteldetailsforbooking *book = [hoteldetailsforbooking new];         if([sectwo.offerallow isequaltostring:@"true"])         {             celltwo.selectedsignlabel.hidden = no;          }         else         {              cellone.selectedsignlabelone.hidden = no;          }         //        [self.detailtableview reloaddata];         nslog(@"price room 1 : %@", sectwo.itempricetext);      }  } 

you need keep track on selection of cell. need store selected indexpath in array.

in viewcontroller.h declare property this

@property(nonatomic,strong) nsmutabledictionary *selectiondata; 

now in viewcontroller.m

- (void)viewdidload {     [super viewdidload];      self.selectiondata=[[nsmutabledictionary alloc]init];  }  - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath {      testtableviewcell *cell = [tableview dequeuereusablecellwithidentifier:@"mycell"];       if ([self.selectiondata objectforkey:[nsstring stringwithformat:@"%ld",(long)indexpath.section] ] != nil) {          nsmutablearray *sectiondata=[[self.selectiondata objectforkey:[nsstring stringwithformat:@"%ld",(long)indexpath.section]] mutablecopy];          if (![sectiondata containsobject:[nsnumber numberwithlong:indexpath.row]])         {             cell.accessorytype = uitableviewcellaccessorynone;             cell.numberlabel.text = @"2";         }         else         {             cell.numberlabel.text = @"***";             cell.accessorytype=uitableviewcellaccessorycheckmark;         }     }     else     {         cell.numberlabel.text = @"2";         cell.accessorytype = uitableviewcellaccessorynone;     }    cell.selectionstyle = uitableviewcellselectionstylenone;     return cell;  }  - (void)tableview:(uitableview *)tableview didselectrowatindexpath:(nsindexpath *)indexpath {      nslog(@"selected section :%li ---> selected row :%li",(long)indexpath.section, (long)indexpath.row);     [self handleselectionforsection:indexpath.section row:indexpath.row];     [self.tablev reloaddata];  }  -(void)handleselectionforsection:(long)sectionindex row:(long)rowindex {       if ([self.selectiondata objectforkey:[nsstring stringwithformat:@"%ld",sectionindex] ] != nil) {          nsmutablearray *sectiondata=[[self.selectiondata objectforkey:[nsstring stringwithformat:@"%ld",sectionindex]] mutablecopy];          if (![sectiondata containsobject:[nsnumber numberwithlong:rowindex]])         {             //removing previous selected rows             [sectiondata removeallobjects];             [sectiondata addobject:[nsnumber numberwithlong:rowindex]];              [self.selectiondata setobject:sectiondata forkey:[nsstring stringwithformat:@"%ld",sectionindex]];         }         else         {             //cell tapped selected,             // can deselect removing object              //if dont want deselect comment following lines             [sectiondata removeobject:[nsnumber numberwithlong:rowindex]];              [self.selectiondata setobject:sectiondata forkey:[nsstring stringwithformat:@"%ld",sectionindex]];         }       }     else     {         //section key not available need create         nsmutablearray *sectiondata=[[nsmutablearray alloc]init];         [sectiondata addobject:[nsnumber numberwithlong:rowindex]];          [self.selectiondata setobject:sectiondata forkey:[nsstring stringwithformat:@"%ld",sectionindex]];      }      nslog(@"all selection : %@",self.selectiondata);  } 

your numberofrowsinsection, numberofsectionsintableview , titleforheaderinsection remain same.

let me know if have query.


Comments

Popular posts from this blog

sequelize.js - Sequelize group by with association includes id -

android - Robolectric "INTERNET permission is required" -

java - Android raising EPERM (Operation not permitted) when attempting to send UDP packet after network connection -