问题描述:

- (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

网友答案:

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

For example: Change

if (type == CellIdentifier)

To

if([type isEqualToString:CellIdentifier])
网友答案:

Try using Switch Condition rather If and else if

网友答案:

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

相关阅读:
Top