The iOS framework that grows only as fast as its documentation
View Recyling

Classes

class  NIViewRecycler
 An object for efficiently reusing views by recycling and dequeuing them from a pool of views. More...
 
protocol  <NIRecyclableView>
 The NIRecyclableView protocol defines a set of optional methods that a view may implement to handle being added to a NIViewRecycler. More...
 
class  NIRecyclableView
 A simple implementation of the NIRecyclableView protocol as a UIView. More...
 

Overview

For recycling views in scroll views.

View recycling is an important aspect of iOS memory management and performance when building scroll views. UITableView uses view recycling via the table cell dequeue mechanism. NIViewRecycler implements this recycling functionality, allowing you to implement recycling mechanisms in your own views and controllers.

Example Use

Imagine building a UITableView. We'll assume that a viewRecycler object exists in the view.

Views are usually recycled once they are no longer on screen, so within a did scroll event we might have code like the following:

for (UIView<NIRecyclableView>* view in visibleViews) {
if (![self isVisible:view]) {
[viewRecycler recycleView:view];
[view removeFromSuperview];
}
}

This will take the views that are no longer visible and add them to the recycler. At a later point in that same didScroll code we will check if there are any new views that are visible. This is when we try to dequeue a recycled view from the recycler.

UIView<NIRecyclableView>* view = [viewRecycler dequeueReusableViewWithIdentifier:reuseIdentifier];
if (nil == view) {
// Allocate a new view that conforms to the NIRecyclableView protocol.
view = [[[...]] autorelease];
}
[self addSubview:view];