Tricks for improving iPhone UITableView scrolling performance?
- Cache the height of the rows (the table view can request this frequently)
- Create a least-recently-used cache for the images used in the table (and invalidate all the inactive entries when you receive a memory warning)
- Draw everything in the
UITableViewCell
'sdrawRect:
if possible avoid subviews at all costs (or if you require the standard accessibility functionality, the content view'sdrawRect:
) - Make your
UITableViewCell
's layer opaque (same goes for the content view if you have one) - Use the reusableCellIdentifier functionality as recommended by the
UITableView
examples/documentation - Avoid gradients/complicated graphical effects that aren't pre-baked into
UIImage
s
How can I speed up a UITableView?
Even if your cell is actually that simple (background image and label) there are some things to consider
Image caching
This is the obvious thing - if you are using the same image everywhere, load it once into the UIImage and reuse it. Even if the system will cache it on its own, directly using the already loaded one should never hurt.
Fast calculation
Another rather obvious thing - make calculating height and content as fast as possible. Don't do synchronous fetches (network calls, disk reads etc.).
Alpha channel in image
What's expensive when drawing is transparency. As your cell background has nothing behind it, make sure that you save your image without alpha channel. This saves a lot of processing.
Transparent label
The same holds true for the label on top of your background view, unfortunately making it opaque might ruin the looks of your cell - but it depends on the image.
Custom cell
In general, subclassing UITableViewCell
and implementing drawRect:
yourself is faster than building the subview hierarchy. You might make your image a class variable that all instances use. In drawRect:
you'd draw the image and the text on top of it.
Check compositing
The simulator has a tool to highlight the parts that are render-expensive because of transparency (green is ok, red is alpha-blending). It can be found in the debug menu: "Color Blended Layers"
How to improve scroll performance when downloading image thumbnail in tableview
- For your issue where the previous image is showing when the cell is reused, you should override
func prepareForReuse()
inPhotoTableViewCell
and setphotoImageView.image = nil
. - For your performance issue, it looks like you're making the API request off the main thread, so that's a good start. How large are the images you're requesting? I'm wondering if the data is so large that it's taking a lot of time to convert it to an image and then set the image on the image view
How To Make Scrolling UITableView Smoother?
This is how you load custom cells
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
CustomCell *cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIndetifier];
}
NSDictionary *article = [entriesArray objectAtIndex:[indexPath row]];
NSString *title = [article objectForKey:@"title"];
NSString *date = [article objectForKey:@"publishedDate"];
NSString *dateEdited = [date substringToIndex:16];
cell.nameLabel.text = title;
cell.dateLabel.text = dateEdited;
NSURL *myURL=[NSURL URLWithString:[self.picturesArray objectAtIndex:indexPath.row]];
NSData *myData1 = [[NSData alloc] initWithContentsOfURL:myURL] autorelease];
UIImage *myImage = [[UIImage alloc] initWithData:myData1] autorelease];
cell.imageView.image = myImage;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.textLabel.numberOfLines = 2;
return [cell autorelease];
}
UITableview scroll freezes and becomes slow with usage
Okay it was from the gesture recognizer as i said. I just replaced it with a button with the same size of the pic. It's on the pic with alpha = 0. Whenever i need the tap recognition, i just activate the alpha of the button which already has an IBAction. Hope this helps...
Related Topics
Instantiate and Present a Viewcontroller in Swift
Fast and Lean Pdf Viewer For Iphone/Ipad/Ios - Tips and Hints
Presenting Modal in iOS 13 Fullscreen
How to Programmatically Get the MAC Address of an Iphone
How to Hide Uinavigationbar 1Px Bottom Line
How to Apply a Perspective Transform to a Uiview
The Simplest Way to Resize an Uiimage
Send and Receive Messages Through Nsnotificationcenter in Objective-C
How to Add Constraints Programmatically Using Swift
How to Set Cornerradius For Only Top-Left and Top-Right Corner of a Uiview
Nsurlsession/Nsurlconnection Http Load Failed on iOS 9
Detecting Which Uibutton Was Pressed in a Uitableview
Dateformatter Doesn't Return Date For "Hh:Mm:Ss"
Appearancewhencontainedin in Swift