inc
inc

Reputation: 205

Prevent UITableView rows from being destroyed

I have a large UITableView and I need to iterate through its cell to extract some information. The problem is that the cells which outside the UITableView bounds are equal null, so I cannot extract any information about them.

Is it possible to prevent UITableView from destroying the cells that are out of bounds (current invisible cells). Modifying the datasource and reloading the whole table is not an option.

Upvotes: 0

Views: 570

Answers (4)

Newsonic
Newsonic

Reputation: 403

You can use embedded UITableView in a UIScrollView to prevent destroying cells.

This works by telling UITableView to reset its content size to the full height.

But with the following conditions:

  1. Disable scrolling of UITableView.

  2. Subclass UITableView with the following class and use it in code/storyboard:

    class IntrinsicTableView: UITableView {
    
     override var contentSize:CGSize {
         didSet {
             self.invalidateIntrinsicContentSize()
         }
     }
    
     override var intrinsicContentSize: CGSize {
         self.layoutIfNeeded()
         return CGSize(width: UIView.noIntrinsicMetric, height: contentSize.height)
     }
    }
    

Upvotes: 0

Antonio MG
Antonio MG

Reputation: 20410

You could copy all the cells you create into a NSMutableArray and keep a reference of them there, in that way the system won't release them, but this is not the way you should work with it.

This can lead to big memory issues if your table is too long, you should think in a solution that saves the data of the cell, not the cell itself.

Upvotes: 2

Nat
Nat

Reputation: 12948

You have to reconsider your approach. Cells consumes a lot of resources so they are usually made reusable - so you don't have to keep them all in memory or destroy/create for each new row. You should have all your data in some array (you can save to array from core data or from web service or whatever). Then, to get any data refer to indexpath.row from your array (objectAtIndex:indexpath.row).

Upvotes: 0

Jakub
Jakub

Reputation: 13860

Technically yes, but you don't want to do that. I mean really! You don't!!

The question is why you need this kind of behaviour, you should be fine using just data source and maybe row count. If you want information about 'invisible cells' you really should look at your NSArray/CoreData or whatever it is what fill your TableView

Upvotes: 1

Related Questions