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.
Captainredmuff
source share