1

friends, i want to add more than one customised cell in a table view in xib file. In detail i want to add different cells for each row in table i tried the below code but seems to be not working i can only customise one cell for all the rows in tableview code is as below i have tried two codes code1 is as below

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{
    NSString *identifier;

    identifier=@"tablecell";
    tablecell *cell = (tablecell*)[tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell==nil)
    {
        NSArray *nib=[[NSBundle mainBundle]loadNibNamed:@"tablecell" owner:self options:nil];
        cell=[nib objectAtIndex:0];
    }
    cell.name.text=@"gopi";
    return cell;


    if (indexPath.row==1)
    {
        NSString *identifier1;
        identifier1=@"gopicell";
        gopicell *cell1=(gopicell*)[tableView dequeueReusableCellWithIdentifier:identifier]; 
        if (cell1==nil)
        {
            NSArray *nib=[[NSBundle mainBundle]loadNibNamed:@"gopicell" owner:self options:nil];
            cell1=[nib objectAtIndex:0];
        }

        cell1.gopilabel.text=@"sabari";
        return cell1;
    }
}

It displays only one customised cell so i tried this code code2

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{
    NSString *identifier;
    if (indexPath.row==0)
    {
        identifier=@"tablecell";
        tablecell *cell = (tablecell*)[tableView dequeueReusableCellWithIdentifier:identifier];
        if (cell==nil)
        {
            NSArray *nib=[[NSBundle mainBundle]loadNibNamed:@"tablecell" owner:self options:nil];
            cell=[nib objectAtIndex:0];
        }
        cell.name.text=@"gopi";
        return cell;
    }
    else if(indexPath.row==1)
   {
       identifier=@"gopicell";
       gopicell *cell1=(gopicell*)[tableView dequeueReusableCellWithIdentifier:identifier]; 
       if (cell1==nil)
       {
           NSArray *nib=[[NSBundle mainBundle]loadNibNamed:@"gopicell" owner:self options:nil];
           cell1=[nib objectAtIndex:0];
       }

       cell1.gopilabel.text=@"sabari";
       return cell1;
   }
    return nil;
}

i know this statement is wrong but i do not know what to return here if i have is it possible to customise more than one cell for each row

  • 1
    check this link http://stackoverflow.com/questions/5575125/adding-multiple-custom-cells-in-uitableview[enter link description here][1] hope it helps. [1]: http://stackoverflow.com/questions/5575125/adding-multiple-custom-cells-in-uitableview – Pradumna Patil Jul 10 '15 at 07:30
  • 1
    can you attach an image. What exactly you want? – Kumar Jul 10 '15 at 07:40

1 Answers1

0

Update your code as below. Check for even and odd cell and put customize cell in Your table view.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{
    NSString *identifier;
    if (indexPath.row %2 == 0)
    {
        identifier=@"tablecell";
        tablecell *cell = (tablecell*)[tableView dequeueReusableCellWithIdentifier:identifier];
        if (cell==nil)
        {
            NSArray *nib=[[NSBundle mainBundle]loadNibNamed:@"tablecell" owner:self options:nil];
            cell=[nib objectAtIndex:0];
        }
        cell.name.text=@"gopi";
        return cell;
    }
    else if(indexPath.row %2 == 1)
   {
       identifier=@"gopicell";
       gopicell *cell1=(gopicell*)[tableView dequeueReusableCellWithIdentifier:identifier]; 
       if (cell1==nil)
       {
           NSArray *nib=[[NSBundle mainBundle]loadNibNamed:@"gopicell" owner:self options:nil];
           cell1=[nib objectAtIndex:0];
       }

       cell1.gopilabel.text=@"sabari";
       return cell1;
   }
    return nil;
}
Kumar
  • 1,882
  • 2
  • 27
  • 44