Background: I am trying to implement a sectioned tableview that can collapse and expand. When collapsed, the table will remember what was selected and preserve the selected state when expanded.
Problem: The a section expanded and function like it should. The cell was selected like it should. However, the cell in the same place but next section appeared to be highlighted as well.
Example: I have 2 sections: A and B. In each section I have 3 cells: a, b and c. I expand both sections so all the cells are displayed on the screen and nothing is selected. I then tap on (A, a). The cell (A, a) is highlighted and all is well. I collapsed section A, then expand section A. (A, a) is highlighted as it should. However, (B, a) is also highlighted as well. This can be repeated with (A, b) and (A, c). It would light up (B, b) and (B, c) respectively.
Project: <Removed>
Code:
viewController.m holds detail matter to this.
@interface ViewController ()
@end
@implementation ViewController{
UITableView * _tableView;
BOOL aOpen;
BOOL bOpen;
BOOL cOpen;
NSIndexPath *selectedIndexPath;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
_tableView.delegate = self;
_tableView.dataSource = self;
_tableView.allowsMultipleSelection = NO;
[self.view addSubview:_tableView];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table View stuff
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 3;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
switch (section) {
case 0:
return [[TableViewHeader alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 40) title:@"Section A" section:section isOpen:aOpen delegate:self];
break;
case 1:
return [[TableViewHeader alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 40) title:@"Section B" section:section isOpen:bOpen delegate:self];
break;
case 2:
return [[TableViewHeader alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 40) title:@"Section C" section:section isOpen:cOpen delegate:self];
break;
default:
return Nil;
break;
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 40;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
switch (section) {
case 0:
return aOpen? 3:0;
break;
case 1:
return bOpen? 4:0;
break;
case 2:
return cOpen? 5:0;
break;
default:
return 0;
break;
}
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
cell.contentView.backgroundColor = [UIColor grayColor];
cell.contentView.alpha = .4;
}
if ([selectedIndexPath isEqual:indexPath]) {//if it was already selected
[tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
}
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 74;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
//for high visibility purpose
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
if (section == 2) {
UIView *view = [[UIView alloc] init];
return view;
}
return nil;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
if (section == 2) {
return 1;
}
return 0;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
selectedIndexPath = indexPath;
[tableView reloadData];
}
#pragma mark - header delegate handler
- (void) headerTappedAtSection: (int) section {
BOOL openAction = false;
int numberOfRow;
switch (section) {
case 0:
aOpen = !aOpen;
openAction = aOpen;
numberOfRow = 3;
break;
case 1:
bOpen = !bOpen;
openAction = bOpen;
numberOfRow = 4;
break;
case 2:
cOpen = !cOpen;
openAction = cOpen;
numberOfRow = 5;
break;
default:
break;
}
[_tableView reloadSections: [NSIndexSet indexSetWithIndex:section] withRowAnimation:UITableViewRowAnimationAutomatic];
if (openAction && numberOfRow) {
[_tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:section] atScrollPosition:UITableViewScrollPositionTop animated:YES];
}
}
@end
I would suggest doing the following:
Remove from tableView: cellForRowAtIndexPath
And add inside header delegate handler function after reloadSection (maybe in if (openAction && numberOfRow){})
This will make sure that cell is selected after the section is reloaded. And since selectedCellIndex is saved, you should be able to use it to select the cell.