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 positi开发者_开发百科oned at the "second row" in the same way that "cell.detailTextLabel.text" appear.
Any suggestions? Thanks in advance.
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;
}
A custom cell is the standard way to do this. There are lots of tutorials for this online, such as this one.
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.
check the tableview suite the sample code
精彩评论