UITableview gets jerky while scrolling because of custom cell creation? - iphone

I have used custom cell class for my uitableview and used in cellForRowAtIndexPath method.
In that method cellIdentifier is already been defined and even cell property is used as dequeueReusable.
But while scrolling my tableview each time my custom cell objects gets created and its methods get called. Because of that on device, scrolling gets jerky effects.
I have also checked if(cell==nil) and inside that i m assigning my image and labels property for that custom cell. But each and every time, this condition gets true and program pointer goes inside that condition.
Please guide me on this.
If possible please provide some example code.
Thanks in advance
Mrunal.

I have pretty complex UITableViewCell. For me the trick was to set the shouldRasterize of the cell layer's to YES. But it introduced blurry text. To avoid that I set the scale - [self.layer setRasterizationScale:[[UIScreen mainScreen] scale]].

As many others have pointed out, we can't really help you unless you post your code (your CellForRowAtIndexPath should be enough).
Generally speaking there are many ways of improving scrolling performance on UITableViews and the top ones I use are (and have used in other Stack Overflow answers):
Always reuse cells, use the dequeuereusablecellwithidentifier when
creating new cells. This prevents the overhead of the OS creating
and destroying lots of objects when scrolling fast.
Collapse the view hierarchy of the cell. Instead of having lots of
views and subviews, create a custom view and do all your cell
drawing in the drawRect. Apps like Twitter use this approach for
super fast cell drawing.
Ensure your images are opaque. You can do this by ensuring all image
assets don't have alpha channels baked into them and by setting the
layer's opaque property to YES.
If you are setting any properties of the cell image's layer property (like shadow or rounded rectangles) this can affect scrolling performance. Setting the layer's shouldRasterize to YES will help, but you should really think hard about doing any intensive drawing operations within cells.
For more details see http://developer.apple.com/library/ios/#samplecode/TableViewSuite/Introduction/Intro.html%23//apple_ref/doc/uid/DTS40007318-Intro-DontLinkElementID_2

This is happening because of ImageView. Create your labels and imageViews inside if(cell == nil) in CellAtIndexPathMehtod of UITableView and assign its value outside this if.
And If your images are loading from server then use Asynchrnous image loading

Related

iPhone: some UITableView's cells appears blank in edit

I use tableView with custom cells and for cell drawing I use drawRect: method for scrolling performance. In edit mode when I move cells move and down sometimes some cells appear blank. And during scrolling also random cells start appear blank.Please look attached images. Can someone help to understand problem?
if you are using dequeueReusableCellForIdentifier then try removing that.
I mean to say alloc a new cell every time.
I am suggesting above based on assumption that your cell contains some heavy data which takes time in drawing. In this case if you reuse cell then it will take cell that goes out of visible screen and will paint draw new data in it.
If this doesn't help then please post some code showing how you are creating custom cell.

How can I make my custom UITableViewCells as fast as possible?

I'm creating custom UITableViewCells, and I'd like to make them in a way that allows the table view's scrolling to be as smooth as possible. In their current state, with a custom background image on each cell using the cell's backgroundView property, scrolling is is still fairly smooth.
But my question is, how can I add content to the cell and maintain this? The cells are fairly different from each other -- one may have a single label, another may have two labels and an icon, and another may have a bunch of other controls.
I've found that using unique cell identifiers for non-similar rows makes the overall experience laggy, so I need a method that allows me to use the same cell identifier and have very different cells.
Should I be using an XIB for this? If not, how should my subclass function? I was thinking of adding all of the controls to the cell, and only hiding/using the ones necessary at the time. Is there a cleaner way?

Sluggish scrolling experience when using QuartzCore to round corners on UIImageView's within a UITableViewCell

I use QuartzCore to add rounded corners to my UIImageView's within the cells of my UITableView
This is the code that I use:
fooImageView.layer.cornerRadius = 9.0;
fooImageView.layer.masksToBounds = YES;
fooImageView.layer.borderWidth = 1.0;
The problem is that when I add this code. The table cells movement is slowed down dramatically. I'm just wondering whether there are other alternatives to make the user experience much faster and to increase performance when scrolling a table view cell using this technique?
I see a lot of applications (most Twitter apps) that have no performance decrease when having rounded corners applied to images within their cells. Just wondering how they overcome the "sluggishness"?
Thanks for your help.
The first thing I would try doing is setting:
fooImageView.layer.shouldRasterize = YES;
This renders the rounded corner effect as a bitmap. I had some similar issues when using CALayer effects on views in a UIScrollView a while back, and this setting drastically improved performance.
Don't forget to set
fooImageView.layer.rasterizationScale = [[UIScreen mainScreen] scale];
to prevent pixelation (rasterization at the wrong resolution for the device).
there are 3 main techniques for improving UITableView performance that I use:
Always reuse cells, use the
dequeuereusablecellwithidentifier
when creating new cells. This
prevents the overhead of the OS
creating and destroying lots of
objects when scrolling fast.
Collapse the view hierarchy of the
cell. Instead of having lots of
views and subviews, create a custom
view and do all your cell drawing in
the drawRect. Apps like Twitter use
this approach for super fast cell drawing.
Ensure your images are opaque. You can do this by ensuring all image assets don't have alpha channels baked into them and by setting the layer's opaque property to YES.
Examples:
In the cellForRowAtIndexPath (the table identifier string simply creates a reference to cells that are the same type and can be anything you like):
static NSString *SimpleTableIdentifier = #"SimpleTableIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: SimpleTableIdentifier];
// Create a new cell if necessary
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:SimpleTableIdentifier] autorelease];
}
Check out the following link for examples of improving performance of UITableViews: http://developer.apple.com/library/ios/#samplecode/TableViewSuite/Introduction/Intro.html%23//apple_ref/doc/uid/DTS40007318-Intro-DontLinkElementID_2
Hope this helps,
Dave
You could do some masking. Overlaying the images with a mask-image that has a rounded corners square cut out from it and it will work like a charm.
Also be sure to use reuseCellIdentifier otherwise the table will lag if it gets even a little complex.
Short answer: set cells opaque and draw them yourself.
Follow the advice by StuDave and Magic Bullet, and see also Fast Scrolling in Tweetie with UITableView by the author of the official Twitter client ot learn how to do cell drawing. It's a simple and clear example project.
Beyond solving this specific issue, you should read UITableView construction, drawing and management (revisited) by Matt Gallagher to learn how to write custom table controllers and cells. This not only improves the performance of your code, it lets you do things which are not possible with the standard classes from Apple. Basically you will create an UIViewController that replicates key methods of the UITableViewController.
In my case it added endless subviews and the table view slowed down dramatically exactly as you said. I added this logic in willDisplayCell to fix the problem:
if cell.contentView.subviews.count < 3 /* elements in cell + subviews */ {
cell.contentView.addSubview(subView)
cell.contentView.sendSubviewToBack(subView)
}

How to increase the scrolling performance in my table view with images in iphone?

I am new to iPhone development. I am parsing a xml and display the title, date, contents and image in the cell. Now the scrolling is not smooth, it is stuck.
How can I increase it? I have already applied lazy loading in another view, I am not able to apply in the new view. So how can I increase the scrolling performance? Can I check for any condition that if image is already loaded in the image view, so I can stop once again the loading of image view?
What I do to guarantee fast scrolling in a table is to subclass my own UITableViewCell where I implement all properties that I need.
Whenever that tablecell is initialized I draw the properties of the cell on the cell itself. So when you need an image on there, dont use an UIImageView, but just draw the image on the cell. Also all the text I need I just draw on there.
After a long stuggle finding out how to do all this, I also found a nice blog post about this.
http://blog.atebits.com/2008/12/fast-scrolling-in-tweetie-with-uitableview/
First of all, check if you're not resizing the images - that takes a lot of computing power, and will slow down the table for sure.
Second of all, check for view hierarchy - complicated view hierarchy means bad performance. Rembemer to use as much opaque views as possible, and when you're using non-opaque views don't make the cells too complex(by complex Apple means three or more custom views). If the views are too complex - use drawRect method for custom drawing of the content.
There's a great example on how to achieve this provided by Apple called AdvancedTableViewCell (here's a link), and there's a great tutorial by Apple about table view cells (another link).

Content of custom table view cells not drawing correctly

I have several parts in my app where I use custom table view cells.
Their content is created with subviews.
The problem is that on some of these cells, the content does not appear at all or does not appear correctly until after the cell was selected for the first time.
One example is a custom cell which has a custom subview which can be set after its creation. This view does not appear at all before I selected the cell and its views were redrawn. Calling -[setNeedsDisplay] in the subview's setter method does not help either.
The problems was that I was using the cells themselves to calculate their height. For some reason, the subviews (which were part of the cell used to calculate the height) weren't appearing correctly in the cells that were used for the actual displaying.
Therefore my advice: Never use a UITableViewCell to calculate its own height. This may work in principle (it doesn't crash), but might bite you later in unexptected and hard-to-debug ways.