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 7651317
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T11:28:26+00:00 2026-05-31T11:28:26+00:00

I want to make a UITableView act like an accordion. When a row is

  • 0

I want to make a UITableView act like an accordion. When a row is tapped it should insert a special row right below the tapped row and then remove any other special row from previous taps. I have tried many things but the code below is my latest attempt.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSIndexPath *removeIndex;
    for (int i = 0; i < [players count]; i++) {
        NSString *player = [players objectAtIndex:i];
        if ([player isEqualToString:@"ADJUST_SCORE_ROW"]) {
            removeIndex = [NSIndexPath indexPathForRow:i inSection:indexPath.section];
            [players replaceObjectAtIndex:i withObject:@"DELETE_ME"];
            break;
        }
    }

    [scoreTableView beginUpdates];
    NSIndexPath *insertPath;
    if (removeIndex && [removeIndex row] < indexPath.row) {
        insertPath = [NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section];
    } else {
        insertPath = [NSIndexPath indexPathForRow:indexPath.row + 1 inSection:indexPath.section];
    }
    [players insertObject:@"ADJUST_SCORE_ROW" atIndex:insertPath.row];

    [scoreTableView insertRowsAtIndexPaths:[NSArray arrayWithObject:insertPath] withRowAnimation:UITableViewRowAnimationTop];

    for (int i = 0; i < [players count]; i++) {
        NSString *player = [players objectAtIndex:i];
        if ([player isEqualToString:@"DELETE_ME"]) {
            [players removeObject:player];
            break;
        }
    }

    if (removeIndex) {
        [scoreTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:removeIndex] withRowAnimation:UITableViewRowAnimationTop];
    }

    [scoreTableView endUpdates];


}
  • 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-05-31T11:28:28+00:00Added an answer on May 31, 2026 at 11:28 am

    The key to this was where the deletes and the inserts happened.

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        Player *selectedPlayer = nil;
    
        //Get rid of any adjustment score row
        NSIndexPath *removeIndex;
        for (int i = 0; i < [players count]; i++) {
            id player = [players objectAtIndex:i];
            if ([player isKindOfClass:[NSString class]] && [player isEqualToString:@"ADJUST_SCORE_ROW"]) {
                if (i == indexPath.row) {
                    [scoreTableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
                    return;  //This is the case where the row that was selected was an adjustment row
                }
                removeIndex = [NSIndexPath indexPathForRow:i inSection:indexPath.section];
                [players replaceObjectAtIndex:i withObject:@"DELETE_ME"];
                break;
            }
        }
    
        selectedPlayer = [players objectAtIndex:indexPath.row];
    
        [scoreTableView beginUpdates];
        NSIndexPath *insertPath;
        if (removeIndex && [removeIndex row] < indexPath.row) {
            insertPath = [NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section];
        } else {
            insertPath = [NSIndexPath indexPathForRow:indexPath.row + 1 inSection:indexPath.section];
        }
    
        HoleScore *holeScore = [[round scoreForPlayer:[players objectAtIndex:indexPath.row] holeGroup:self.holeGroupIndex andHole:hole.holeIndex] objectForKey:CURRENT_HOLE_SCORE];
        if (holeScore == nil) {
    
            NSEntityDescription *scoreEntity = [NSEntityDescription entityForName:@"HoleScore" inManagedObjectContext:moc];
            holeScore = [[HoleScore alloc] initWithEntity:scoreEntity insertIntoManagedObjectContext:moc];
            [holeScore setPlayer:selectedPlayer];
            [holeScore setHoleGroupIndex:[NSNumber numberWithInteger:holeGroupIndex]];
            [holeScore setHoleIndex:[NSNumber numberWithInteger:hole.holeIndex]];
    
            [holeScore setStrokes:[NSNumber numberWithInteger:[hole par]]];
            [holeScore setPuts:[NSNumber numberWithInteger:2]];
    
            HoleScoreCardTableViewCell *playerCell = (HoleScoreCardTableViewCell*)[scoreTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section]];
            [playerCell setHoleScore:holeScore];
    
            [round addScoresObject:holeScore];
    
            NSError *error;
            [moc save:&error];
    
            if (error) {
                NSLog(@"Error saving: %@", error.localizedDescription);
            }
    
        }
    
        for (int i = 0; i < [players count]; i++) {
            id player = [players objectAtIndex:i];
            if ([player isKindOfClass:[NSString class]] && [player isEqualToString:@"DELETE_ME"]) {
                [players removeObject:player];
                break;
            }
        }
    
        if (removeIndex) {
            [scoreTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:removeIndex] withRowAnimation:UITableViewRowAnimationTop];
        }
    
        [players insertObject:@"ADJUST_SCORE_ROW" atIndex:insertPath.row];
    
        [scoreTableView insertRowsAtIndexPaths:[NSArray arrayWithObject:insertPath] withRowAnimation:UITableViewRowAnimationTop];
    
        [scoreTableView endUpdates];
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to use insertRowsAtIndexPaths method to insert a row to a UITableView, but
The design of the UITableView is something like this: I want to make my
I want to make the grouped UITableView transparent. I partially succeded with the following
I have a UITableView with multiple sections, and I want to make only one
I want make an iphone app that has a UITableView that displays different texts
I would like to make my UITableView Footer stop floating over my content, as
How to make UITableView like above image? I know this is grouped table type.
Just want to make sure I'm not overlooking something obvious... It seems like it
I want to make a menu for my app like bottom menu of Camera+
I want to make a UITableView, which has many columns, so the whole cell

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.