Creating a reusable UIView similar to a UITableViewCell

Ahoy!

I am trying to create a reusable UIView (for various reasons) similar to the UITableViewCell implementation used in the UITableViewController. I would like to use a reusable view in UIScrollView, so I know that I am not trying to achieve something completely unattainable.

Default implementation:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { //declare cell identifier static NSString *cellIdentifier = @"cell_identifier"; //dequeue cell UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; //check cell is valid if(cell == nil) { //create a new cell cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier]; } // //return cell return cell; } 

From this it should be noted that the cell has been removed from the UITableView. If the cell is invalid, a new cell is created. My question is, how is this cell then queued again for reuse?

My current implementation attempt is as follows:

 - (TestScrollViewCell *)scrollView:(TestScrollView *)_scrollView cellForRowAtIndexPath:(NSIndexPath *)indexPath { //declare cell identifier static NSString *cellIdentifier = @"cell_identifier"; //dequeue cell TestScrollViewCell *cell = (TestScrollViewCell *)[scrollView dequeueReusableCellWithIdentifier:cellIdentifier]; //check cell is valid if(cell == nil) { //create a new cell cell = [[TestScrollViewCell alloc] initWithFrame:CGRectZero]; } // //return cell return cell; } 

I think adding NSMutableDictionary to my TestScrollView to store cellIdentifier and TestScrollViewCell (UIView) and then throwing them back based on the dictionary key would be a good start, but is this really a true reusable Cell implementation?

The problem that I see is that I am adding a UIView to the ScrollView, which is positioned based on the frame. Removing this view in this sense would not allow me to add the view to the scroll view without affecting the first view (by changing the frame), but, of course, does this work with UITableViewCells, as well as with section headers / footers?

I looked at this implementation , which seems to follow the same route that I intended to implement, but I'm not 100% sure that this is a real implementation of reusable cells.

Is anyone lucky with this before? I'm trying to get Apple to do this, but apart from UITableViewCell and MKAnnotationView (MapKit), there are no implementations available to me.

Any help would be greatly appreciated.

+7
source share
1 answer

This is not just a view, it is the entire UITableViewController that you will need to recreate. The reuse stream looks like this: dequeueReusableCell gets an empty reused cell from some store, I think, from NSMutableArray (grab the first object from the array and then remove it from the array and return it). If the array is empty, the method returns nil. You check the value of the cell, if it is zero, you create a new instance of the cell class. If it is not zero, you populate it with your data. This applies to each visible cell, that is, each cell that can fit on the screen. Any invisible cells are not initialized. When a user scrolls through a table, a cell that is completely disabled (one pixel is not displayed) is sent again to reuse Queue - all their sub-items and values ​​are returned to their default values ​​or simply filled, and then the cell is added to the end of our NSMutableArray, this is the queue. Hope I explained quite well. EDIT: Oh, and one more thing - you will need different reuse queues for each reuse identifier.

+3
source

All Articles