Multiple UITableViewCell classes in one UITableView?

I am compiling a TableView, and I need to have several cell classes used in the same table.

So, for example, how do I use more than one class of cells in my cellForRowAtIndexPath method?

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; // Configure the cell... return cell; } 

I know that I could just replace the UITableViewCell with my own class and use the if statements to set the class depending on the index, but is it not so messy that it would be a sensible and perhaps the most intelligent way to do this?

+6
source share
3 answers

Of course. Just create new instances of your custom cells and give them a new CellId.

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { if (condition1) { static NSString *CellIdentifier1 = @"Cell1"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1]; // TODO: if cell is null, create new cell of desired type. // This is where you create your custom cell that is // derived form UITableViewCell. } else { static NSString *CellIdentifier2 = @"Cell2"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2]; //TODO: if cell is null, create new cell of desired type } // Configure the cell... return cell; } 
+9
source

You can do this by pointing out the table for multiple prototypes in your .xib or Storyboard file. Pay attention to the Dynamic Prototypes setting for the table view in the Xcode screenshot below:

enter image description here

Each prototype cell must be assigned a unique reuse identifier. In this example, the first cell type has a reuse identifier @"ScanCell" , and the second @"DetailCell" . Then in your method -tableView:cellForRowAtIndexPath: you simply choose which cell class to use, choosing which reuse identifier you switch to -dequeueReusableCellWithIdentifier:

Here is an example taken from one of my own applications:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSString * cellIdentifier = @"DetailCell"; if ([indexPath section] == 0) { cellIdentifier = @"ScanCell"; } UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath]; if ([indexPath section] == 1) { CBPeripheral * peripheral = (CBPeripheral *)[self.appDelegate.peripherals objectAtIndex:[indexPath row]]; cell.textLabel.text = [peripheral name]; cell.detailTextLabel.text = [self.appDelegate.peripheralKeys objectAtIndex:[indexPath row]]; } return cell; } 

If you want the prototype cells to have different classes, just set their class in the .xib or Storyboard file.

+6
source

At some point, you will need to associate different classes of cells with different types of elements in your data source. The if may be a way. You can encapsulate this in a separate method, for example:

 +(Class)cellClassForItem:(id)rowItem { Class theClass = [ UITableViewCell class ] ; if ( [ rowItem isKindOfClass:[ ... class ] ] ) { theClass = [ CellClass1 class ] ; } else if ( [ rowItem isKindOfClass:[ ... class ] ] ) { theClass = [ CellClass2 class ] ; } return theClass ; } 

Or you can have every element in your a protocol data source:

 @protocol DataSourceItem <NSObject> -(Class)tableViewCellClass ; @end 

Your delegation method will now look like this (assuming a second technique)

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { id<DataSourceItem> item = (id<DataSourceItem>)[ tableView itemForRowAtIndexPath:indexPath ] ; Class cellClass = [ item tableViewCellClass ] ; NSString * cellID = NSStringFromClass( cellClass ) ; UITableViewCell *cell = [ tableView dequeueReusableCellWithIdentifier:cellID ] ; if ( !cell ) { cell = [ [ cellClass alloc ] initWithStyle:... reuseIdentifier:cellID ] ; } cell.value = item ; return cell; } 
+3
source

Source: https://habr.com/ru/post/921351/


All Articles