I have several tables in my application in which the data is retrieving and tables updated dynamically. I noticed on the device, when on screens which had more cells than the screen could fit, so required a scroll, it would lag while scrolling.
I started going through my code line by line to find the problem until I finally found it
CachedImages *cache = [[CachedImages alloc] init];
UIColor *backgroundColour;
if([table getImage] != nil)
{
backgroundColour = [[UIColor alloc] initWithPatternImage:[cache getImageByPath:[[table getImage] getName]]];
}
cell.backgroundColor = [backgroundColour colorWithAlphaComponent:fOpacity];
(I have a table class which stores table info such as background image and data)
Its this line in particular
cell.backgroundColor = [backgroundColour colorWithAlphaComponent:fOpacity];
if I comment out that one line, the table scrolls perfectly, but when I uncomment it, it causes the lag issue. So in order round this, I set up a instance variable which stored the background colour for the table and removed the cached images code from the delegate methods, so it is loaded before the table is, so it doesn’t have to keep pulling it from cache everytime a cell needs redrawn.
This has mostly fixed the issue, when I first access the screen and scroll down, it is a bit laggy, but after the initial scroll to the bottom, the performance is fine. Is there anyway to prevent this initial lag, I have moved all the heavy rendering out of the delegate methods, but this image code is still causing a slight lag the first time the user enters the screen.
Any ideas on how to prevent this? The table is a custom table, and I is set up like this
NSString *CellIdentifier = @"CustomCellIdentifier";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCellView" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
So I am reusing the identifier.
Any suggestions would be much appreciated
Best solution I could come up with is preloading the image prior to the screen appearing, so it doesn’t have to keep fetching it from cache. This has mostly fixed the issue, the first time the screen loads there is a slight lag, but after that its smooth. Best solution I have found so far