Reputation: 151
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
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
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
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