Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8804185
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T01:32:17+00:00 2026-06-14T01:32:17+00:00

Background: I am trying to implement a sectioned tableview that can collapse and expand.

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T01:32:18+00:00Added an answer on June 14, 2026 at 1:32 am

    I would suggest doing the following:

    Remove from tableView: cellForRowAtIndexPath

    if ([selectedIndexPath isEqual:indexPath]) {//if it was already selected
         [tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
    }
    

    And add inside header delegate handler function after reloadSection (maybe in if (openAction && numberOfRow){})

         [_tableView selectRowAtIndexPath:selectedIndexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to implement a design in CSS that will have a tiled background
Background I am trying to implement a RESTful web service using Apache-CXF that interacts
I am trying to implement background music into an Android game that I am
I am trying to implement a long running background process that is spawned when
I am trying to implement a gps location listener that will stay on and
The background is : I am trying to implement an automated integration test solution.
I have an Asp.Net background and trying to learn Silverlight. Can you explain, what
Background: I'm trying to come up with a regex for a rewrite rule that
Question How can I dynamically resize an image in ASP.NET MVC? Background I'm trying
Background: I'm trying to implement a messenging system in my app, and I'm writing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.