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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T22:49:14+00:00 2026-06-03T22:49:14+00:00

I want to make a typical situation: when user selects any cell, it’s accessoryType

  • 0

I want to make a typical situation: when user selects any cell, it’s accessoryType turns in checkmark. Only one cell’s accessoryType can be checkmark. And then I wanna save in NSUserDefaults indexPath.row so my app will be able to know which cell user selected and make some changes in options. So I wrote this wrong code:

didSelectRowAtIndexPath

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // checkedIndexPath is NSIndexPath

    if(self.checkedIndexPath)
    {
        UITableViewCell* uncheckCell = [tableView
                                        cellForRowAtIndexPath:self.checkedIndexPath];
        uncheckCell.accessoryType = UITableViewCellAccessoryNone;
    }
    UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.accessoryType = UITableViewCellAccessoryCheckmark;

    self.checkedIndexPath = indexPath;

    [[NSUserDefaults standardUserDefaults]setObject:[NSNumber numberWithInt:self.checkedIndexPath.row]forKey:@"indexpathrow" ];
} 

cellForRowAtIndexPath

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Part of code from cellForRowAtIndexPath

    if(indexPath.row == [[[NSUserDefaults standardUserDefaults]objectForKey:@"indexpathrow"]intValue ])
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
    else 
    {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }

        return cell;
}

However, this code works badly. When you open the UITableView, there is an already selected cell in the table and when you press another there are two checkmarked cells…How can I improve my code or should I change it whole ? Any suggestions ?
Thanks !

  • 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-03T22:49:16+00:00Added an answer on June 3, 2026 at 10:49 pm

    Try this code:

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        // checkedIndexPath is NSIndexPath
        NSIndexPath *previousSelection = self.checkedIndexPath;
        NSArray *array = nil;
        if (nil != previousSelection) {
            array = [NSArray arrayWithObjects:previousSelection, indexPath, nil];
        } else {
            array = [NSArray arrayWithObject:indexPath];
        }
    
        self.checkedIndexPath = indexPath;
    
        [tableView reloadRowsAtIndexPaths:array withRowAnimation: UITableViewRowAnimationNone];
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        // Part of code from cellForRowAtIndexPath
        NSString *cellID = @"CellID";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
        if (nil == cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
            [cell autorelease];
        }
    
    // some code for initializing cell content
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
        if(self.checkedIndexPath != nil && indexPath.row == self.checkedIndexPath.row)
        {
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
        } else {
            cell.accessoryType = UITableViewCellAccessoryNone;
        }
    
        return cell;
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want make interactive application where user launches it and can do various task
I want make a typical tabbar based application. And I want to insert login
I want make datetimepicker in my project. Using jquery how it is possible? I
I want make a bash script which returns the position of an element from
Let's say I want make some of my sources publicly available via my blog
I am new android app developer i want make app for tablets and phone
I have style sheet with a class name changebackgroundcolor i want make change in
Sorry google translate make mistake, to many mistake, i want make an alarm in
I am traversing a HTML document using javascript DOM. I want make a list
I have link for example domain.com/de/controler/action?param=value and I want make actionlink to keep same

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.