Amar Syla
Amar Syla

Reputation: 3663

How can I display a thumbnail image to Table Cell in xCode?

As a Xcode beginner, I want to display a thumbnail in the table cell in my app. For now, I have this code which parses JSON data and posts it in the title and subtitle of the cell.

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

    if(cell == nil){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"MainCell"];
    }

    cell.textLabel.text = [[news objectAtIndex:indexPath.row] objectForKey:@"receta"];
    cell.detailTextLabel.text = [[news objectAtIndex:indexPath.row] objectForKey:@"koha"];

    return cell;
}

How can I show a thumbnail in the right of the cell?

Thanks.

Upvotes: 1

Views: 2308

Answers (1)

jszumski
jszumski

Reputation: 7416

If you don't want to make a custom cell, you can create a UIImageView with your desired image and set it as the cell's accessoryView, which will show it on the right edge of the cell. You also need to ensure that the height of the cell is tall enough to fit the image's height.

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 100;
}

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    cell.accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"some-image-name"]];

    return cell;
}

You can return a constant (I chose 100) if the image will always be a fixed size, or you can check the UIImage's size and return something like size.height + 10 for some extra padding.

Upvotes: 3

Related Questions