I have a grouped UITableView in my UIViewController class and I'm finding that at times, the dequeued cell is not nil as expected.
The table has 3 sections to start with and and as soon as the 'viewDidLoad' is invoked, a server call is initiated to find out if there are more sections. Before the view is even rendered, the server's response arrives and we're told that we have 4 sections. To deal with this change, I do:
[self.tableview beginUpdates]
// Do the updating of the array that holds table data
[self.tableview endUpdates]
[self.tableview reloadData];
Next I get the call to 'numberOfSectionsInTableView', for which I return 4, followed by 'numberOfRowsInSection' and I'm returning the expected correct row count. Note this is the first time I'm getting either of these calls since the table reload happened so quickly and before the view was even rendered.
At this point, only the data from the 1st 3 sections are visible and scroll to view the last section. When you scroll to see the last section, one of the cells I'm expecting to be in the call to 'tableView:cellForRowAtIndexPath' is not nil as expected. The cell type is actually what was used for another section (I have one type of UITableViewCell for the 1st two sections and another for the last two sections that I'm creating and handing back in cellForRowAtIndexPath when cell is nil).
So how does UITableView figure out which cell to dequeue and how do I figure out why this cell for this particular section/row is not nil when it really should be?
What are you using for your cell identifier? That's what a UITableView will use when determining what to dequeue. If you have different types of cells, you’ll need different reuse identifiers.
The idea of cell reuse boils down to minimizing view allocations and internal UITableViewCell setup in order to improve scrolling performance. Roughly speaking, whenever a cell goes off screen, a table view removes it from itself as a subview and adds it to a pool. The next time it needs to display a cell, you can dequeue an unused cell from the pool, configure it and return it to the table view. That's a clever implementation, but a table view cannot efficiently reuse cells without your hints, so called reuse identifiers.
The most common approach is to tie reuse identifiers to cell classes. If, say, you use cells of class A to display people from an address book and cells of class B to display organizations, you obviously cannot reuse cells B for people, and vice versa. In such a case you designate these two classes different reuse identifiers, which guarantees that when you need to provide cell A, the table will either dequeue a cell A or return nil if its pool of unused cells A is empty.
A natural extension to that is nil reuse identifiers. They tell a table view that it should not reuse them at all, releasing them when no longer on screen. Why may you need it? For unique cells or for the cells which you manage yourself, or for the cells which have an untypical life cycle, or are extremely expensive to draw. However, the fewer unreusable cells you have, the lower memory footprint.
Related
I am having a view to display a list of Shops within a UITableView.
The user should have the possibility to modify the shops displayed by some filters.
For example it should be possible to display the shops ordered by name (with the first letter as section header). Further the user should be able to display just the favourites or sort the shops by category (category name is section header):
I did not want one table view, handling all the filters because I thought it would be a mess of if-then-else tags depending on the current filter. So I decided to create 4
UITableViewControllers with each of them handling one filter.
That's exactly what I need, no redundancy so far, because the tableVC delegate and datasource methods are the ones which need to be implemented differently.
But now I do need a header which appears depending on the scroll position of the table. This is exactly the same in all of the 4 tables. But how should I solve this issue now?
Implement the UIScrollViewDelegate methods in each of the four UITableViewControllers redundant? Create a ParentViewController handling the scrollView methods (which would not be so easy because of the table datasource and delegate methods...
Any suggestions how to solve this properly? How would you do this?
I would appreciate any help, thanks in advance.
I would create a parent class, taking care of the header and derive the UITableViewControllers for the filters from it.
The superclass takes care of the common elements (like the header), the subclasses take care of the actual display.
Alternatively, you can create an overloaded method, which returns an Array of cells to display.
Store this array and when the delegate (in the superclass) asks for the 'cell data', give him the corresponding element in the array.
My table cells consist of a UIView on the left side, a label in the center, and a label on the right.
I add new rows with
insertRowsAtIndexPaths:withRowAnimation:
The rows I add look just fine. But when each is added, an existing cell loses its UIView, but keeps both labels.
What could be causing this? The views keep a positive retain count the entire time, and the cell identifiers are all unique. Also, when I scroll down and then up, so that the cells have to be redrawn, the UIViews are again visible. When the row is added, cellForRowAtIndexPath is definitely only called on the new row.
I would really check the memory/retain logic. By the way, how are you sure that your retain count is correct? DO NOT RELY in retain count since it does not do as you expect it. I tried many times before and it is not reliable and therefore even often missleading.
Do you use ARC already?
Are you by any chance reusing the same variables (directly or indirectly) to create your UIiew?
Just for testing purposes make sure you really retain the views (for example add them to an mutablearray or so) and see what happens.
Also if you post some code, it will be much easier for us to help!
This may be a very basic question. But I am finding it confusing with my tableView acting weird when I scroll up and down.
So UITableViewCells are Reusable !! So each row in my tableViewCell is reusable, and so, if I scroll down, I tend to use the same cells for different purposes (as programmed). My assumption is that 'a cell is a row'
Are sections reusable as well ?
So if I have 6 sections with 2 displayed on the screen at the moment, and I scroll down.... Do I effectively have 2 sections being reused thrice or do I have 6 sections ?
A Section is just visual appearance of groups of UITableViewCells in a UITableView. It is not a object or something that one can reuse.
Another reason why sections can not be made reusable is that, its rare that two sections contain same number of rows of same kinds of cells.
The only reusable elements in a table view are UITableViewCells.
As I understand it a section is not an object where a cell is. Therefore you do not need to give a section any memory as you do when you allocate a cell so do not need to recycle the sections in order to save memory. They are just indices for which section you are in. So "no" I think is the answer.
Perhaps if you posted your code we could be more help.
Martin
Cells are rows, yes. Sections themselves aren't reusable, but you can dictate which kind of cell gets reused for each section using the reuseIdentifier. That may give you the behavior you're looking for.
I have a UITableView which reloads every 3 seconds. I reload it only if my Boolean variable is true. In some conditions that variable is always true and that time table is difficult to scroll. (performance is not very good). Other times its ok. What should I do?
Note: I have coded my table according to apple's recommended way(UITableView best practises. Except I add subviews to UITableViewCell, instead drawing on it. I'm confident with other techniques).
What's the solution for this?
Are you sure that because you refresh a lot that makes you difficult to scroll. What kinds of refreshing you mean here, refreshing data from network or refresh the table view your cell.
Everytime you refresh your table view or when you scroll the table view, the table view will keep asking for the corresponding cell, and now the whole performance depends on how fast you return the cell.
If you are doing custom UITableViewCell, you are in risk of having a slow performance. Double check these things:
Do you reuse your cell correctly? Check if [tableView dequeueReusableCellWithIdentifier:] always return nil or not. If it always return nil, then you do it wrongly.
Check if you block the main thread somewhere by loading images from network or file, if it is, using multithread.
After you check everything and still has a slow performance, then you either need to reduce the times a cell is returned (by less refreshing) or draw the cell yourself
If you are fetching large data set for UITableView, try to fetch the data on need basis. Get the data for each cell in cellForRowAtIndexPath instead of getting all the data for table view and storing.
For the app I'm currently working on, one of my main ViewControllers contains two UITableViews whose contents vary based on what's stored in my model (Core Data backed by SQL). Both tableviews set their delegate and datasource properties to the shared VC they're both subviews of, and I provide the necessary methods in the VC to respond to tableview-specific events.
I'm finding that when I add new data to my database that should cause the TableViews to show new rows, and I return to the VC and call reloadData on both TableViews in the VC's viewWillAppear method, I see that numberOfRowsInSection returns the correct number of rows based on the new entries in CoreData, but cellForRowAtIndexPath is not called the correct number of times, almost like it's not responding to the change in the model data. For example, if I had 3 rows to show then the app started, and I added another entry in CoreData, then called reloadData, numberOfRowsInSection returns 4 correctly, but cellForRowAtIndexPath is only called 3 times still.
Besides the fact that I probably be using something more suitable like NSFetchedResultsController to link my TableViews to CoreData, is there any reason why cellForRowAtIndexPath is not being called the correct number of times, as dictated by the correct number of rows returned by numberOfRowsInSection?
I would put in some of my code, but it's really standard boilerplate TableView stuff and I feel like there's something else that I'm missing.
Just to make sure you understand correctly, the cellForRowAtIndexPath: is called with the number of cell that can be in the screen. If you have 4 cells, but because the height of the cell, only 3 cells can be available to users, then the cellForRowAtIndexPath: is called 3 times
If the user can only see only 3 cells, not more, then the cell loading callbacks may only be called 3 times, even if there are 100's of rows in your table.