I have a table view with 5 sections and I have set the tableview selection to multiple. Each section have different number of rows. What I want is to set that the user can select only one cell from each section(in my table user can select any number of cells). ex: 5 cells from 5 sections.
It should be impossible to select more than one cell from any section. If user select another cell from same section, previously selected cell should be deselected. How can I do this. This is a 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 for two sections to 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 for 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 for room 1 : %@", sectwo.itempriceText);
}
}
You need to keep track on the selection of cell. So you need to store selected
indexpath
in array.in
ViewController.h
declare property like thisNow in
ViewController.m
Your
numberOfRowsInSection
,numberOfSectionsInTableView
andtitleForHeaderInSection
will remain same.Let me know if you have any query.
Looks like you are updating celltwo / cellone selectedsignLabel.hidden on table selection. so @Lion solution will not working. You have to save the last selected index using below code :
I have not update the complete working code. But this is the way to achieve. You can also use the userdefault instead of self.selectedIndexPathDict.
You can set
selection
property of tableview from interface builder. Select your tableview in IB and then selectattribute inspector and set
single selectionto
selection` property like below screenshot.Or you can set programattically,
Update :
If you want single selection per section then you can implement
willSelectRowAtIndexPath
as below,In this case you should allow multiple selection in
tableview
i think.Reference : Answer of John Sauer