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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T22:39:42+00:00 2026-05-23T22:39:42+00:00

Our UITableView returns the number of rows for a given section. What we’re experiencing

  • 0

Our UITableView returns the number of rows for a given section. What we’re experiencing is by the time cellForRowAtIndexPath is called, the number has changed so we end up getting array index out of bounds.

Is there a good way of syncing these two methods so the underlying data is not changed? We considered using @synchronized, but not sure when you would release the lock.

One other thing we’re doing to refresh the table is this from a separate thread.

[self addUsers:usersToShow];       
[[self invokeOnMainThreadAndWaitUntilDone:NO] refresh:self]; // is this the issue?


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.users.count;  // returns 10 for example
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *newCell= nil;

    static NSString *cellId = @"cellId";

    cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (cell == nil) {
        [[NSBundle mainBundle] loadNibNamed:@"MyCell" owner:self options:nil];
        cell = newCell;
        self.newCell = nil;
    }

    User* user = [self.users objectAtIndex:indexPath.row]; // index out of bounds now because number of users has changed
}
  • 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-23T22:39:43+00:00Added an answer on May 23, 2026 at 10:39 pm

    As you’ve kind of worked out for yourself, you can’t use @synchronized because the scope extends beyond the method.

    You don’t want to be trying to use a Lock object, locking it in numberOfRowsInSection and unlocking it cellForRowAtIndexPath. It’s not going to work. What you need to do is ensure that you do any locking you need to in cellForRowAtIndexPath and handle the fact that the row passed in might not be valid anymore e.g.:

    User * user = nil;    
    @synchronized(self.users) {
            if (indexPath.row < [self.users count]) {
                 user = [self.users objectAtIndex:indexPath.row];
            }
    
    }
    
    if (user) {
        //configure cell
    }
    else {
        //set cell fields to be blank
    }
    

    Have you tried updating the model (self.users) only in the main thread? This should reduce the likely hood of your updates to the model from interleaving with calls to getNumberOfRows and configureCellAt. In the example you’ve given you are updating the model on a random thread and then reloading the data on the main thread. I’d recommend ensuring that you’re model is thread safe (or updated/read in only the main thread).

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

Sidebar

Related Questions

Our app has a UITableViewController with some custom section headers. These section headers are
Our app has changed every month. It is very hard to modify sections and
Our ASP.NET 3.5 website running on IIS 6 has two teams that are adding
Our dev shop currently uses Visual SourceSafe. We all know how that could end
Our Tfs server (Tfs2008) has smtp server installed. But we could not make Tfs
I am using a UiTableView in our application.We have two tabs in controller one
Our application has a lot of .NET assemblies, which up until now, has not
Our current web application url reveals the class package structure to the end user.
Our company has recent started switching from Blackberries to iPhones and the biggest complaint
Our app (before authentication) has Customers and Widgets. As you'd expect each customer has

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.