TableView with multiple prototype cells

21,053

Solution 1

If you are using three prototype then use three identifiers. Because only one identifier will cause problem. And you will get wrong result. So code like this.

if(indexPath.row==0){
 // Create first cell
}

if(indexPath.row==1){
 // Create second cell
}

else{
 // Create all others
}

You can use switch case also here for best performance.

Solution 2

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

    if (cell.tag == 0) 
   {
    return array1.count;
   }
   else(cell.tag == 1)
   {
    return array2.count;
   }    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
 static NSString *cellIdentifier;

 NSString *membershipType = [membershipTypeArray objectAtIndex:indexPath.row];

 if ([membershipType isEqualToString:@"silver"]||[membershipType isEqualToString:@"gold"])
 {
     cellIdentifier = @"cell";
 }
 else if ([membershipType isEqualToString:@"platinum"])
 {
     cellIdentifier = @"premiumCustomCell";
     cell.customCellImageView.image = [cellImageArray objectAtIndex:indexPath.row];
 }

 cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

 if (!cell) {
     cell = [[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
 }
 cell.selectionStyle = UITableViewCellSelectionStyleNone;
 cell.headingLabel.text = [titleArray objectAtIndex:indexPath.row]; 
}

Solution 3

Here i wrote code like:

#pragma mark == Tableview Datasource

  - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  return 2;
 }

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSInteger nRows = 0;
switch (section) {
    case 0:
        nRows = shipData.count;
        break;
    case 1:
        nRows = dataArray1.count;
        break;
    default:
        break;
}
return nRows;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *cellIdentifier = @"cellIdentifier1";
NSString *cellIdentifier1 = @"cellIdentifier2";
SingleShippingDetailsCell *cell;
switch (indexPath.section) {
    case 0:
        cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        //Load data in this prototype cell
        break;
    case 1:
        cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier1];
        //Load data in this prototype cell
        break;
    default:
        break;
}
return cell;
 }
Share:
21,053
David West
Author by

David West

Updated on November 09, 2020

Comments

  • David West
    David West over 3 years

    I had a simple question regarding a table view with 3 different kinds of prototype cells. The first two occur just once while the third occurs 4 times. Now what I'm confused about is how to specify in my cellforRowatindexpath which cell prototype to use for which row. So, I want something like for row 0, use prototype 1, for row 1, use prototype 2, for rows 3,4,5 and 6 use prototype 3. What's the best way to do this? Do i give each prototype an identifier and then use dequeueReusableCellWithIdentifier:CellIdentifier ? Can you'll provide some sample code?

    EDIT:

    Still not working. This is the code I have at the moment. ( I only have one case for the switch statment because I just want to test and see if the cell is being generated in the first row or not, but currently table view is blank)

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
         switch(indexPath.row)
    {          
     case 0: {static NSString *CellIdentifier = @"ACell";
                       UITableViewCell *cell = [tableView
                                               dequeueReusableCellWithIdentifier:@"ACell"];
      if(cell==nil) {
        cell=[[UITableViewCell alloc]
              initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"ACell"];
    
                            }
      return cell;
      break;
        }
      }
    }
    

    Acell is my identifier for a cell prototype that I created. I

  • aks.knit1108
    aks.knit1108 over 11 years
    During creating cell you can follow the same methods, before you are using with single identifier. But here you will use three identifier during deque.
  • Suraj K Thomas
    Suraj K Thomas about 10 years
    how to handle no. of rows in section. if you have 2 prototype cells and no of rows to be displayed based on array count?
  • Ashish P
    Ashish P about 10 years
    you just have to tag your custom prototype cell and check your array count for each cell to return 'array.count'