3

I have 3 custom cells to be displayed in one tableview of 50 rows. I found a reference which satisfies my need at Multiple Custom Cells dynamically loaded into a single tableview

It seems it gets complicated to create objects for cells. As per my need 3 cells do same functionality but views are different,

can we use a factory pattern to build cells?

Is there any implementation for this kind of pattern?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
      // I would like to create object some like this
      CustomCell *cell = factory.getCell("customCell1", tableView);

}

I have the class diagram for custom cells. enter image description here

Community
  • 1
  • 1
satyanarayana
  • 265
  • 4
  • 14

2 Answers2

3
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell<CustomCellProtocol> *cell = [factory getCellForIndexPath:indexPath tableView:tableView];

    // Getting data for the current row from your datasource
    id data = self.tableData[indexPath.row]; 
    [cell setData:data];

    return cell;
}

// Your Factory class.

- (UITableViewCell<CustomCellProtocol> *)getCellForIndexPath:(NSIndexPath *)indexPath tableView:(UITableView *)tableView
{
    UITableViewCell<CustomCellProtocol> *cell;
    NSString *cellNibName;
    if (condition1) {
        cellNibName = @"CustomCell1"; //Name of nib for 1st cell
    } else if (condition2) {
        cellNibName = @"CustomCell2"; //Name of nib for 2nd cell
    } else {
        cellNibName = @"CustomCell3"; //Name of nib for 3th cell
    }

    cell = [tableView dequeueReusableCellWithIdentifier:cellNibName];

    if (!cell) {
        UINib *cellNib = [UINib nibWithNibName:cellNibName bundle:nil];
        [tableView registerNib:cellNib forCellReuseIdentifier:cellNibName];
        cell = [tableView dequeueReusableCellWithIdentifier:cellNibName];
    }

    return cell;
}
arturdev
  • 10,884
  • 2
  • 39
  • 67
  • thank you for your reply, the above code works for dequeuing cell by identifiers but in the same way I want to create a factory which create a new cell from Xib and dequeues those cell, right? – satyanarayana Jul 26 '14 at 12:13
2

A factory method isn't appropriate because it doesn't allow you to dequeue.

Register each custom class for reuse with your table view (viewDidLoad is a good place to do this):

[self.tableView registerClass:[CustomCell1 class] forReuseIdentifier:@"customCell1"];
// Repeat for the other cell classes, using a different identifier for each class

In cellForRowAtIndexPath, work out which type you want, then dequeue:

CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
[cell setData:data]; // all subclasses can do this

A new cell will be made, or returned from the pool if possible.

jrturton
  • 118,105
  • 32
  • 252
  • 268
  • Thank you for your reply, If I send tableView reference to factory then factory can dequeue the cells, CustomCell *cell = factory.getCell("customCell1", tableView); – satyanarayana Jul 26 '14 at 12:10
  • 1
    @satyanarayana Factory methods are simply not the correct way to do this. You are already getting reusable queue behaviour from the table view. – duci9y Jul 26 '14 at 12:19
  • @duci9y is absolutely right. The table view will do the job of your "factory", and also take care of re-use. Don't fight the frameworks. – jrturton Jul 26 '14 at 12:51