Rama Krishna
Rama Krishna

Reputation: 1

UITableView cell having different identifiers for three different cell design

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

    static NSString *CellIdentifier = @"reviews";
    static NSString *CellIdentifier1 = @"recipes";
       static NSString *CellIdentifier2 = @"easy_tips";
    // Configure Cell

    NSString *type = [[myaray objectAtIndex:indexPath.row]objectForKey:@"type"];
    NSLog(@"types====%@",type);

    UITableViewCell *mycell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    if (type == CellIdentifier) {

    firstCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

        NSLog(@"First type %@", type);

    return cell;
    }else if (type == CellIdentifier1){


    SecondTableViewCell *cell1 =  [tableView dequeueReusableCellWithIdentifier:CellIdentifier1 forIndexPath:indexPath];


        return cell1;

    }else if (type == CellIdentifier2){

    ThirdTableViewCell *cell2 =  [tableView dequeueReusableCellWithIdentifier:CellIdentifier2 forIndexPath:indexPath];

        return cell2;
    }

    return mycell;
}

I am getting only one cell every time it is first cell .

It is not going to if condition statement. Thanks in advance for your reply

Upvotes: 0

Views: 371

Answers (3)

geet Sebastian
geet Sebastian

Reputation: 687

I think it would be better if you consider rethinking the whole logic.

I would do the same thing like this

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *type = [[myaray objectAtIndex:indexPath.row]objectForKey:@"type"];
    myCell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:@"CustomCellid" ];
    if(myCell==nil)
    {
        NSArray *arr= [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];

        if(types isEqualToString:@"reviews")
        {
           myCell=[arr objectAtIndex:0];    
        }
        else if(types isEqualToString:@"recipes")
        {
           myCell=[arr objectAtIndex:1];
        }
        else if(types isEqualToString:@"easy_tips")
        {
           myCell=[arr objectAtIndex:2];
        }

   }


return myCell;

}

Create a UITableViewCell class 'CustomCell'. Create three UITableViewCells in 'CustomCell.xib'. Each cell for each type, the whole xib will be having the identifier 'CustomCellid' Then the above code would give you desired result. give a try

Upvotes: 0

Muhammad Ahsan
Muhammad Ahsan

Reputation: 297

Try using Switch Condition rather If and else if

Upvotes: 0

Leo
Leo

Reputation: 24714

Change every part you use == to compare string to isEqualToString:

For example: Change

if (type == CellIdentifier)

To

if([type isEqualToString:CellIdentifier])

Upvotes: 2

Related Questions