Billy Shih
Billy Shih

Reputation: 624

Why can't I set an image to my UITableViewCell?

I created an option menu using a UITableViewController and can get the labels to set correctly, but not the images.

I tried taking out the switch statement and using an array filled with the correct values based on [indexPath row] and could not get it to work correctly either.

Any ideas?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier];
    }

    switch ([indexPath row]) {
        case 0:
            [[cell imageView] setImage:[UIImage imageNamed:@"42-photos.png"]];
            [[cell textLabel] setText:@"Photos"];
            break;
        case 1:
            [[cell imageView] setImage:[UIImage imageNamed:@"280-clapboard.png"]];
            [[cell textLabel] setText:@"Videos"];
            break;
        case 2:
            [[cell imageView] setImage:[UIImage imageNamed:@"42-info.png"]];
            [[cell textLabel] setText:@"About"];
            break;
        default:
            return cell;
            break;
    }

    return cell;
}

Upvotes: 0

Views: 263

Answers (2)

Abhishek Singh
Abhishek Singh

Reputation: 6166

You have not created a custom cell i guess , it's better to have a custom cell if you have so many controls in your UITableViewCell and then accessing the property to the controls you can change the image.you can refer this post of mine.Your style chosen will not support image.

Upvotes: 0

Mick F
Mick F

Reputation: 7439

The UITableViewCellStyleValue2 doesn't include the image:

A style for a cell with a label on the left side of the cell with text that is right-aligned and blue; on the right side of the cell is another label with smaller text that is left-aligned and black. The Phone/Contacts application uses cells in this style.

You should use UITableViewCellStyleDefault instead (or design your own cell).

Upvotes: 2

Related Questions