This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Lazy load images in UITableViewCell
Ive got a problem with my UITableView cells and it is that my cells are reloading each time they go out from the screen. My app loads images from a url and place them in a ImageView in the left part of the cell, so it makes the UITableView scroll really slow.
I was wondering if there is a way to:
1.Load all the cells at once.
2.Disable the reload of the cells.
In this way the TableView could scroll smoothly.
Each cell is composed of three subViews. Two are used for the title and description label and the last one for the image. The contents come from a NSXMLParse so loading this cells takes time.
Thanks in advance.
Instead of using uiimageview use sdwebimage. It will help you I think.
The three ways I can think of to improve the scrolling are to:
Not load the images in the main thread, just stick in a place holder and have them filled in when the data finally becomes available (lazy loading as mentioned in the comment).
Cache the images yourself so when the cell is viewed later you already have the data to display.
Pre-fetch anticipated images into your cache for the cells above and below the displayed rows.
Of course these are not mutually exclusive and should likely be combined to get the smoothest user interface.
Related
I am working on a iphone app, and I am just learning iphone app development for weeks, so I have some questions here : this app will load many posts from a remote server, and the post is consist of one text field, one image, and three buttons (good, bad and comment). There are thousands of posts on the server, so I thought the app will load three posts each time, and when the user scroll up the page, it will continue to load the previous post, and if the user scroll down the page, it will continue to load the next post. For being a newbie, I don't know what View is good for it, any advice is appreciated.
I have tried :
UIView + UIScrollView + Label / Image / Buttons (post)
UIView + UITableView + UITableViewCell (Label, image, buttons)
The first one is fine, and the second one, I really don't know how to make it in the second option..so what view pattern is good for this?
You are going to want to use a UITableView for almost all content in a list view. UITableView recycles is cells to save memory, so it can handle far more items than the just a bunch of uiviews in a scrollview I would start by reading up on the UITableview docs, and looking up some tutorials on custom UITableView cells.
here is the docs:
http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UITableView_Class/Reference/Reference.html
personally I have a little library that I setup with custom UITableViewCell (via subclassing) to do this. You will also want to setup a model that works with the paging of your remote server (3 at a time you stated). So on the last post you will want to add a custom load more cell that when tapped, will get page 2 then lather,rinse,repeat. Any questions let me know.
You should go with the UITableView option. To tell if the user has scrolled to the bottom, look at this link.
Also, start the easy way. Just use the standard UITableViewCell and set the detailText label to your downloaded post title. Once you figure this out along with handling the scrolling and automatically downloading new posts then update the cell to handle images and additional content.
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.
I have a Nib file containing grouped table view and a cell. The cell is connected to UITableViewController through outlet. In cellForRowAtIndexPath I just return that cell and I can see a single cell in the table view. But when I change the row count of table to 2 and want to show the same cell, then I can see only one, it appears that the second cell is intended to be there, as the lower corners of the visible cell are not rounded, however, it's not there.
If I create a second cell object in nib file, second outlet and return it as second row, the it appears fine. My cell has identifier specified in IB.
Does it mean I can't re-use cell object for more than one row?
In the same way as you would need one instance of a UIButton for each visible button in your view, you will need one instance of your cell for each visible row.
The common pattern to manage this, is to ask the tableview for a previously instantiated cell that is no longer needed (dequeueReusableCellWithIdentifier:), and then return that cell. If the table view does not have any reusable cells, you have to instantiate a new one from your nib file.
There are many examples on this around the web, and you can also find some here at SO, ex in this answer.
I would recommend that you read through Apples TableView Programming Guide, which also contains a section on loading cells from nibs.
UPDATE:
An attempt on explaining the TableView and reuse of cells in a different way.
Lets say we have a large gallery with
old paintings. Thousands of paintings.
The gallery has just one display room,
though, and it has walls for just ten
paintings. The gallery manager has to
switch paintings now and then when the
visitors get bored and want to see
some new paintings.
Every displayed painting needs a
frame. Without a frame, it can't be
put on a wall. Frames are expensive to
make, and take up a lot of space. The
frame maker guy want have time nor
money to build the thousands of frames
needed.
He finds out that he want be needing
frames for all the paintings that is
not shown at the moment. He would only
need ten frames for the currently
displayed paintings. When the gallery
manager takes down a painting, the
frame maker stores the frame, and when
the gallery manager put up a new
painting and asks the frame maker for
a frame for it, the frame maker
returns the frame from the previous
painting again.
One day, the
needed-space-between-paintings-regulations
gets changed for no good reason. The
gallery manager is able to put up two
more pictures in the display room. He
picks two paintings from the store
room, and asks the frame maker for
frames. The frame maker has no spare
frames, and need to make two new
frames.
Now, lets say that the gallery is a TableView, and all the paintings are rows of data. The display room with space for ten visible paintings, is the screen, with space for ten visible rows. Each visible row would need a cell, just like each displayed painting would need a frame.
In the end, you shouldn't care that much about saving resources by reusing one cell. That's TableViews responsibility. It's an implementation detail of the TableView how many cells is needed and how it is used. The protocol defines how you can ask the TableView for an reusable cell, and the documentations states that you should. That should be enough. Demo projects shows that TableView can manage very large amounts of data. If your projects struggles with performance because of instantiating 10-20 cells from nib, you probably got some problems with your nib file or something. There are some discussions, though, about the performance of loading from nib versus building cells in code. It may be interesting to you.
I had some very weird behavior that sounds very much like what you are describing some time ago.
Eventually I found that the problem was that I had just added a table view cell to a xib which contains other items such as the parent table view and controller. What I had to do was create a seperate xib for each table view cell individually. I think the issue was that loading the table view cell from an incorrectly built xib was confusing the issue.
As Vegar said there are a lot of tutorials on how to do it.
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).
In my app UItableView with custom cells, cells with image, but in some cells has not image.
In start the place without image is white. After scrolling table in that places appears others picture. Please help to fix this problem.
although your answer is rendering a working solution, I think it is not the correct way to deal with this problem.
Imagine an NSArray with a 1,000 NSStrings in it that you want to display using a table. By showing them in a cell with a unique id for that NSString, you are basically creating a 1,000 cells! This would slow down your app. The whole idea is that cells are reused so that only 1-2 more than displayed are created.
So the correct solution: deal with it.
reuse a cell by having a unique id just for that class
reset/renew content of the cell yourself, assume dirty content
ps watch the Stanford iPhone class on tables, number 8 (around 18:50), a guy from Apple explains all this.
http://www.stanford.edu/class/cs193p/cgi-bin/index.php
Your problem here is due to the way your are using the cell queue in the UITableView, I think you are not giving each one of your cells a unique cell identifier, because of this you are experiencing that pictures swap from one cell to another.