Walid Hossain
Walid Hossain

Reputation: 2714

iOS TableView loads data very slowly

I'm working on a project that parse data from web service and shows in a table view. Everything is ok, but I'm not satisfied with the performance of tableview. After parsing data form web I called reload data to show the data, But it is not showing cells immediately. It shows the data after 10/15 seconds later. I've checked all data's are loaded before I called reload data. And the strange thing is that it shows the cells immediately if I try to drag the table.

Any idea?

Update

-(void)receivedCategories:(NSMutableArray *)categoryItems{
  [self.spinner stopAnimating];
  self.categories=categoryItems;
  if (self.tableView!=nil) {
    [self.tableView reloadData];
  }
}

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
  return [self.categories count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  static NSString *CellIdentifier = @"CategoryCell";
  CategoryCell    *cell           = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  cell.category                   = [self.categories objectAtIndex:indexPath.row];
  return cell;
}

CategoryCell.m

@implementation CategoryCell

- (void)setCategory:(Category *)category
{
  [self.categoryTitle setText:category.title ];
  [self.categorySubTitle setText:category.description];
}

@end

Upvotes: 2

Views: 7955

Answers (5)

Franco Noack
Franco Noack

Reputation: 11

Are you using custom fonts?

I had a very similar problem, where my UITableView was taking considerably long to load. I finally found the solution, and it didn't had anything to do with my code or the way I was loading custom cells.

The problem occurred at run time, when the system loaded my custom UITableViewCells (with predefined custom fonts) from the storyboard, and because the custom fonts where not correctly installed, the system would look all over the place for the font files, causing a considerable lag when loading the file.

To make sure your fonts are correctly installed:

  1. Include your fonts in your XCode project
  2. Make sure that they’re included in the target
  3. Double check that your fonts are included as Resources in your bundle
  4. Include your iOS custom fonts in your application plist
  5. Find the name of the font (To find out the correct name, you can print all installed fonts)

    for (NSString* family in [UIFont familyNames]){
    
        NSLog(@"Family %@", family);
    
        for (NSString* name in [UIFont fontNamesForFamilyName: family]){
    
            NSLog(@"\t Font%@", name);
        }
    }
    
  6. Use UIFont and specify the name of the font

    [UIFont fontWithName:@"FONT_NAME" size:size];
    

Upvotes: 1

Luis Delgado
Luis Delgado

Reputation: 3734

I had the same problem, the time lag between [tableview reloaddata] and the app calling the method [cellForRowAtIndexPath] was too long (a couple of seconds). Putting the [reloaddata] method inside a dispatch_async(dispatch_get_main_queue(), ^{} solved the issue.

Upvotes: 1

Rok Jarc
Rok Jarc

Reputation: 18865

It seems you're not calling [self.tableView reloadData]; from the main thread and that could be the cause of your problem.

You could try:

[self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:YES];

Also check the accepted answer here for more info.

Upvotes: 24

Atif
Atif

Reputation: 1220

When you create CategoryCell? In first time dequeueReusableCell will return nil. So then make the Category cell.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  static NSString *CellIdentifier = @"CategoryCell";
  CategoryCell    *cell           = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  if (!cell) {
    cell = [[[CategoryCell alloc] init] autorelease];   }

  cell.category                   = [self.categories objectAtIndex:indexPath.row];
  return cell;
}

Upvotes: -1

J2theC
J2theC

Reputation: 4452

With the amount of code you are showing (none), I would have to say that the performance hit you are having is because you are calling reloadData with a very large number of records, or because you are doing some time consuming parsing/calculation when creating the cell. Also, If you are showing images on those cells, that might also cause performance problems.

Upvotes: 0

Related Questions