views:

8

answers:

1

I have a table view where I want a different image for each cell based on logic. Here is my cellforrowatindexpath code:

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

    static NSString *MyIdentifier = @"MyIdentifier";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
                                       reuseIdentifier:MyIdentifier] autorelease];
    }


    NSInteger *row = [indexPath row];
    NSString *platform = [[myStringArray objectAtIndex: row];
    cell.imageView.image = [self imageForInventory:platform];

    return cell;
}

Here is my imageForInventory method:

- (UIImage *)imageForInventory:(NSString *)platform {   
    if (platform = @"Win") {
        UIImage *winImage = [UIImage imageNamed:@"Vista.png"];

        return winImage;
    }else if (platform = @"Mac") {
        UIImage *appleImage = [UIImage imageNamed:@"Apple.png"];
        return appleImage;
    }else if (platform = @"Linux") {
        UIImage *linuxImage = [UIImage imageNamed:@"Linux.png"];
        return linuxImage;
    }   

    return nil;
}

This shows the winImage for every row ignoring the if statements. How do I get the image to set accordingly per cell?

+1  A: 

It's probably your comparison code, to compare strings in imageForInventory use [str1 isEqualToString:str2]. You're using = which is the assignment operator right now, but even if you change it to == it wouldn't work.

Tejaswi Yerukalapudi