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

  • Home
  • SEARCH
  • 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 8722131
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:20:28+00:00 2026-06-13T07:20:28+00:00

I have a tableView and I’m tracking which rows were selected by adding the

  • 0

I have a tableView and I’m tracking which rows were selected by adding the row’s indexPath to an array and then adding a checkmark.

I’m iterating through that array using this bit of code in my cellForRowAtIndexPath method and it works like a charm:

for (NSIndexPath * element in indexArray)
    if ([indexPath compare:element] == NSOrderedSame) { 
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }

However, I’m also taking the value of that row and putting it into another array for other purposes. I figured I could simplify my code if I just used this second array for both cases but I don’t understand how I’d rewrite the above code. I get the concept but the above code uses an indexPath comparison and I’ll need to compare a string to the array.

It’s that ‘if’ line that I don’t know how to change.

for (NSString *element in valueArray) {
    if ([indexPath compare:element] == NSOrderedSame) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
}  

Thoughts? Thanks.

Edit With More Info

My array contains 400 unique items that will not change. Adding their indexPath to my array and iterating through it to add checkmarks works fine. The reason why I want iterate using the array’s string values instead is because I want to add a searchBar to the tableView.

After I add that searchBar, when I do a search, the searchResultsTableView truncates my 400 unique items down to the few that match that search – so we’re down to one or a few rows in my searchResultsTableView. Now let’s say we select the first row that was returned in the search – my code then adds that row’s indexPath to my indexArray to keep track of checkmarks. It would add the index 0,0.

But then when we cancel out of Search mode, the first row 0,0 would be checked in my tableView because we selected 0,0 in searchResultsTableView – even though the row we selected would be way down the list in the full tableView. See where I’m going? So my thought was to track the row’s string instead of it’s indexPath to alleviate this problem.

More clear?

  • 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-13T07:20:29+00:00Added an answer on June 13, 2026 at 7:20 am

    You could use an array of (mutable) dictionaries as data source for the table view. Each dictionary contains the text value and the selection state (as NSNumber) for one row. (I’m assuming here that your table view has only one section).

    Then in cellForRowAtIndexPath you can do something like

    NSDictionary *d = [dataArray objectAtIndex:indexPath.row];
    NSString *value = [d objectForKey:@"value"];
    BOOL selected = [[d objectForKey:@"selected"] boolValue];
    

    and set the cell’s content and accessory type accordingly.

    To change the selection state:

    NSDictionary *d = [dataArray objectAtIndex:indexPath.row];
    BOOL selected = [[d objectForKey:@"selected"] boolValue];
    [d setObject:[NSNumber numberWithBool:!selected] forKey:@"selected"];
    

    Now for your search you would create a filtered subset of that array. But because the filtered array references the same dictionaries, the selection state for each entry is preserved when you change the search text or finish the search.

    Of course you would have to use the filtered array in the above methods when the search is active.

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

Sidebar

Related Questions

I have TableView which number of rows depends on the number of NSStrings in
I have a tableView with many values. I want to get the first indexPath.row
I have a tableview on which selecting a column of a row will call
I have a TableView in which I can scroll to view all the rows.
I have tableview, where create new rows and set them layout manually. In every
I have a tableview and an array source. When I init array in viewDidLoad
I have tableView with a lot of cells. I need to remove cells, which
I have tableview which holds a list of data, when users press Edit i
I have a tableview with UISwitch in each row of the table. Switch is
I have a tableView with sections, which could be opened and closed. So, when

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.