Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TableView with multiple prototype cells

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

like image 747
David West Avatar asked Feb 07 '13 18:02

David West


People also ask

What is IndexPath in Tableview Swift?

indexPath(for:)Returns an index path that represents the row and section of a specified table-view cell.

How do I populate Uitableview?

There are two main base ways to populate a tableview. The more popular is through Interface Building, using a prototype cell UI object. The other is strictly through code when you don't need a prototype cell from Interface Builder.

What is IndexPath Uitableview?

IndexPath contains information about which row in which section the function is asking about. Base on this numbers you are configuring the cell to display the data for given row.


1 Answers

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.

like image 93
aks.knit1108 Avatar answered Sep 19 '22 11:09

aks.knit1108