4
votes

I'm trying to create a tableview which contains different types of table cell but I'm stuck. How do I return a different type of cell dependent on the row number in the...

  • (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

function.

2

2 Answers

3
votes

All you gotta do is switch on the row or section of your nsindexpath and return the appropriate UITableView cell...lets say u have TableViewCellA and TableViewCellB one for row 0 and other for row 1

(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
     UITableViewCell *cell;
    if(indexPath.row==0)
   {
     cell=..//tableviewcellA 
   }
   if(indexPath.row==1)
    cel=///tableviewcell b 
}

and so forth, hope this helps

1
votes
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 

{ 

NSString *identifier=@"MyCell";

if (indexpath.section==0)

{
 customCell1 *cell1=(customCell1 *)[tableView dequeueReusableCellWithIdentifier:identifier];

    if (cell1==nil)
    {
        NSArray *nib=[[NSBundle mainBundle]loadNibNamed:@"customCell1" owner:self options:nil];
        cell1=[nib objectAtIndex:0];
    }
return cell1;

}

if (indexpath.section==1)

{
 customCell2 *cell2=(customCell2 *)[tableView dequeueReusableCellWithIdentifier:identifier];

    if (cell2==nil)

{
        NSArray *nib=[[NSBundle mainBundle]loadNibNamed:@"customCell2" owner:self options:nil];
        cell2=[nib objectAtIndex:0];
    }
return cell2;

}