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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T14:27:43+00:00 2026-06-10T14:27:43+00:00

I have a UITableView that should have 33 rows. Each row represents a specific

  • 0

I have a UITableView that should have 33 rows. Each row represents a specific time slot in a day. When the view that holds the table view loads, I need it to populate each row accordingly.

I have an array of reservation objects that gets passed to the view. Each reservation contains a slot number, a reservation name and the duration of the reservation in slots.

What is the best way to populate the table, I am currently iterating through the array of reservations in the - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath method.

This is not giving me the results or the behavior I am expecting. The performance is extremly poor as it keeps iterating through loops and cells that shouldn’t be blue are blue after scrolling. What is the best way to approach this? I have included the code below.

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 33;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    NSString *timeStamp = [NSString stringWithFormat:@"%.2f", (indexPath.row + 14.0 ) / 2.0];
    timeStamp = [timeStamp stringByReplacingOccurrencesOfString:@".50" withString:@":30"];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    cell.textLabel.text = [NSString stringWithFormat:@"%@: ", timeStamp];

    for (Reservation *temp in bookingsArray) {
        if ((temp.slotNumber - 1) == indexPath.row) {
            cell.textLabel.text = [NSString stringWithFormat:@"%@: %@", timeStamp, temp.reservationName];
            cell.contentView.backgroundColor = [UIColor blueColor];
        }
        for (NSNumber *tempNo in temp.slotIdentifiers) {
            if ([tempNo intValue] -1 == indexPath.row) {
                //cell.textLabel.text = [NSString stringWithFormat:@"%@: Booked", timeStamp];
                cell.contentView.backgroundColor = [UIColor blueColor];
            }
        }
    }

    return cell;
}

UPDATE
Trying the following gives me strange behaviour where all the cells turn blue after I start scrolling.

- (void)viewDidLoad
{
    [super viewDidLoad];
    bookManager = appDelegate.bookingManager;
    bookingsArray = [[NSArray alloc] initWithArray:[bookManager getBookingsForCourt:1 onDate:[NSDate date]]];
    namesArray = [[NSMutableDictionary alloc] init];
    slotIndexSet = [NSMutableIndexSet indexSet];

    for (int c = 0; c < 33; c++) {
        [namesArray setObject:@"Available" forKey:[NSNumber numberWithInt:c]];
    }

    for (Reservation *temp in bookingsArray) {
        [namesArray setObject:temp.reservationName forKey:[NSNumber numberWithInt:temp.slotNumber]]; 
        for (NSNumber *slotNo in temp.slotIdentifiers) {
            [slotIndexSet addIndex:[slotNo intValue] + 1];
        }
    }

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    NSString *timeStamp = [NSString stringWithFormat:@"%.2f", (indexPath.row + 14.0 ) / 2.0];
    timeStamp = [timeStamp stringByReplacingOccurrencesOfString:@".50" withString:@":30"];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    cell.textLabel.text = [NSString stringWithFormat:@"%@: ", timeStamp];
    cell.textLabel.text = [namesArray objectForKey:[NSNumber numberWithInt:indexPath.row]]; 

    if ([slotIndexSet containsIndex:indexPath.row]) {
       cell.contentView.backgroundColor = [UIColor blueColor]; 
    }

    return cell;
}
  • 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-10T14:27:45+00:00Added an answer on June 10, 2026 at 2:27 pm

    You need to do two things to speed this up:

    • Convert bookingsArray to a bookingBySlotNumber array in such a way that the object at index i has slotNumber - 1 equal to i. You can do it by iterating over the original bookings array when you receive it.
    • Create a NSIndexSet called isBookedBySlotNumber containing indexes of items that have been booked. You can prepare it by going through all Reservation.slotIdentifiers, and marking the indexes of isBookedBySlotNumber for items that have been booked.

    With these two pre-processed items in place, you can eliminate the nested loops altogether: the outer one will be replaced by a lookup in bookingBySlotNumber, and the inner one – by a loopup in isBookedBySlotNumber.

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

Sidebar

Related Questions

I have a UITableView consisting of three rows - each row is a data
I have a UITableView with many rows, each row contains a UIWebView. The content
I have a UITableView that displays single large images in each cell. The names
I have a uitableview that loads fairly large images in each cell and the
If I have a UITableView that has 4 rows in it then shouldn't the
I have a UITableView settings screen with options important enough that I should be
I have the following method that should populate the cells of my UITableView with
I have 2 views that contain a UITableView each. They are both displayed at
I have a project: first view is uitableview, when you select row uinavigationcontroller pushes
I have a simple function that should check if the view is at the

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.