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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:31:34+00:00 2026-06-13T09:31:34+00:00

I’m trying to setup my table so that the user can select one item

  • 0

I’m trying to setup my table so that the user can select one item per section.

For example:

- Section 0
    - Row 0 √
    - Row 1
- Section 1
    - Row 0
    - Row 1 √
    - Row 2

So in the above example if the user selects section 0, row 1 then row 0 in the same section should be unchecked and the selected row gets a checkmark.

Same goes for section 1 where any selected row should get a checkmark and then I want to remove the checkmark from the previously selected row in the same section.

- Section 0
    - Row 0
    - Row 1 √
- Section 1
    - Row 0
    - Row 1
    - Row 2 √

Please keep in mind that I won’t have a predefined number of sections or rows, so the code should work for this scenario. Here’s what I currently have, hopefully that might get me started on the right path.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    MyObject *myObject = [self.fetchedResultsController objectAtIndexPath:indexPath];
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
    if ([myObject.optionSelected boolValue] == NO) {
        [myObject setOptionSelected:[NSNumber numberWithBool:YES]];
        [cell setAccessoryType:UITableViewCellAccessoryCheckmark];
    } else {
        [myObject setOptionSelected:[NSNumber numberWithBool:NO]];
        [cell setAccessoryType:UITableViewCellAccessoryNone];
    }
    if ([tableView numberOfRowsInSection:indexPath.section] > 1) {
        NSMutableArray *cells = [[NSMutableArray alloc] init];
        for (NSInteger i = 0; i < [tableView numberOfRowsInSection:indexPath.section]; ++i) {
            [cells addObject:[tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:indexPath.section]]];
        }
        for (UITableViewCell *deselectCell in cells) {
            if ([self.tableView indexPathForCell:deselectCell] != indexPath && deselectCell != cell) {
                MyObject *tempObject = [self.fetchedResultsController objectAtIndexPath:[self.tableView indexPathForCell:deselectCell]];
                [tempObject setOptionSelected:[NSNumber numberWithBool:NO]];
                [cell setAccessoryType:UITableViewCellAccessoryNone];
            }
        }
    }
}

As you can see I am also setting the object’s selected state, I’d like this to remain intact 🙂

Thanks for any feedback and help in advance!

  • 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-13T09:31:35+00:00Added an answer on June 13, 2026 at 9:31 am

    Your tableview should declare a mutable array to hold the currently selected paths:

    NSMutableArray *selectedCellPaths = [[NSMutableArray alloc] init];
    

    Then your tableView:didSelectRowAtIndexPath: should look like this

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
        MyObject *myObject = [self.fetchedResultsController objectAtIndexPath:indexPath];
        UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
        if ([myObject.optionSelected boolValue] == NO) {
            [myObject setOptionSelected:[NSNumber numberWithBool:YES]];
            [cell setAccessoryType:UITableViewCellAccessoryCheckmark];
            [selectedCellPaths addObject:indexPath];
        } else {
            [myObject setOptionSelected:[NSNumber numberWithBool:NO]];
            [cell setAccessoryType:UITableViewCellAccessoryNone];
            if ([selectedCellPaths containsObject:indexPath]) {
                [selectedCellPaths removeObject:indexPath];
            }
        }
    
        // Now we're going to remove all but the cell path that is actually selected.
    
        NSMutableArray *cellsToRemove = [[NSMutableArray alloc] init];
        for (NSIndexPath *selectedCellIndexPath in selectedCellPaths) {
            if ([selectedCellIndexPath compare:indexPath] != NSOrderedSame && selectedCellIndexPath.section == indexPath.section) {
            // deselect cell at selectedCellPath
                [cellsToRemove addObject:selectedCellIndexPath];
            }
        }
        [selectedCellPaths removeObjectsInArray:cellsToRemove];
    }
    

    Note I have just put in a comment where you would want to deselect the actual cell at the cell path that is not selected. You need to fill that code in yourself.

    I haven’t tested this code, just modified what you had in TextMate but it should work barring minor changes.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to select an H1 element which is the second-child in its group
I'm trying to create an if statement in PHP that prevents a single post
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a small JavaScript validation script that validates inputs based on Regex. I

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.