Dan
Dan

Reputation: 151

How to add picture in UITableViewCell

I'm new to Iphone application development and objective C. I wonder if there is a way to insert a picture in a UITableViewCell - with or without creating a custom cell?

I would like to have it positioned at the "second row" in the same way that "cell.detailTextLabel.text" appear.

Any suggestions? Thanks in advance.

Upvotes: 1

Views: 1972

Answers (4)

Marichka
Marichka

Reputation: 397

You can simply add UIImageView with your picture as cell's subview. It will work both ways --with and without creating a custom cell.

Here's some sample code:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *kCellID = [NSString stringWithFormat:@"%d", indexPath.row];

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellID];

    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellID] autorelease];

    UIImageView *MyImageView = [[UIImageView alloc] initWithFrame:CGRectMake(100.0, 33.0, 300.0, 110.0)]; //put your size and coordinates here

    MyImageView.image = [UIImage imageNamed:@"MyImage.png"];
    [cell.contentView addSubview: MyImageView];
    [MyImageView release];

    return cell;
}

Upvotes: 1

madhavi
madhavi

Reputation: 174

check the tableview suite the sample code

Upvotes: 0

Nava Carmon
Nava Carmon

Reputation: 4533

In your table controller:

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

    static NSString *CellIdentifier = @"CellIdentifier";

    UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier: CellIdentifier];
    if (cell == nil) 
        cell = [[UITableViewCell alloc] initWithFrame:CGZeroRect];

    CGRect cellFrame = cell.frame;
    NSInteger offetXInCell = 3, offsetYInCell = 7;        

    cellFrame.origin.x += offsetXInCell;
    cellFrame.origin.y += offsetYInCell;

    [cell setFrame:cellFrame];

    UIImage *newImage = [UIImage imageNamed:@"MyPicture"];
    cell.image = newImage;

    // .... other initializations of cell
    return cell;
}

I wonder if it will work. You should put your numbers in offsets.

Upvotes: 0

Marcelo Cantos
Marcelo Cantos

Reputation: 186098

A custom cell is the standard way to do this. There are lots of tutorials for this online, such as this one.

Upvotes: 0

Related Questions