Larry
Larry

Reputation: 1

iOS Objective C - UITableView performance issue

My app is using CoreData as persistence data storage. Below is my code for the tableview. On the simulator it runs fine, but when running it on the phone it gets very laggy. Any suggestions on optimization is appreciated :)

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

CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
Journal* journal = [self.fetchedResultsController objectAtIndexPath:indexPath];

cell.titleLabel.text = journal.title.uppercaseString;
cell.titleLabel.font = [UIFont fontWithName:@"SourceSansPro-Bold" size:25];
cell.titleLabel.textColor = [UIColor blackColor];

cell.detailLabel.text = journal.detail;
cell.detailLabel.font = [UIFont fontWithName:@"SourceSansPro-SemiBold" size:18];
cell.detailLabel.textColor = [UIColor blackColor];

NSDate *currentDate = journal.timeStamp;

cell.dateLabel.text = [self.dateFormatter stringFromDate: currentDate];
cell.dateLabel.font = [UIFont fontWithName:@"SourceSansPro-SemiBold" size:16];
cell.dateLabel.textColor = [UIColor blackColor];

cell.locationLabel.text = [NSString stringWithFormat:@"%@, %@", journal.city, journal.country];
cell.locationLabel.font = [UIFont fontWithName:@"SourceSansPro-SemiBold" size:18];
cell.locationLabel.textColor = [UIColor blackColor];

cell.tempLabel.text = [NSString stringWithFormat:@"%g°C", round(journal.temp)];
cell.tempLabel.font = [UIFont fontWithName:@"SourceSansPro-SemiBold" size:18];
cell.tempLabel.textColor = [UIColor blackColor];
cell.weatherIcon.image = [UIImage imageNamed:journal.condition];

cell.backgroundView = [[UIImageView alloc] initWithImage:[ [UIImage imageWithData:journal.image] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ];
cell.backgroundView.contentMode = UIViewContentModeScaleAspectFill;
cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:[ [UIImage imageWithData:journal.image] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ];
cell.selectedBackgroundView.contentMode = UIViewContentModeScaleAspectFill;
cell.backgroundView.alpha = 0.5;
cell.selectedBackgroundView.alpha = 0.5;

return cell;
}

Upvotes: 0

Views: 93

Answers (2)

Tao.Yu
Tao.Yu

Reputation: 1

First, you can customize a cell, some code is unnecessary, such as set Font and TextColor in every time. Next, you can use instrument-->Profile to find the code where is time consuming. Hope it can help you!

Upvotes: 0

Evgeny Karkan
Evgeny Karkan

Reputation: 9662

You are experiencing low FPS lags because of this API usage:

[UIImage imageWithData:journal.image]

The UIImage imageWithData: method is NOT asynchronous, so as the table view loads each new cell, it has to process the data, locking up the app while doing so.

Try to find an async way to load/create/cache an image on a background thread making your UI responsive.
Check out this popular image loading/caching library SDWebImage for more ideas/inspiration and potential solution for your needs.

Upvotes: 1

Related Questions