I would like to know if it is possible to adjust the width of a UITableViewCell. I would like to put an image to the left of it (like the Contact view in Address Book). I found a post here that provides a picture of exactly what I am trying to accomplish. I would also like to confirm the answers to this post.
OS3.0 has some style options in the API that may do what you want.
Or, you can create a completely custom table view cell within Interface Builder, for example in one of my apps I do this in my cellForRowAtIndexPath:
PlaceViewCell *cell = (PlaceViewCell*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [self createNewPlaceCellFromNib];
}
And this is the method to dig it out from the NIB
- (PlaceViewCell*) createNewPlaceCellFromNib {
NSArray* nibContents = [[NSBundle mainBundle]
loadNibNamed:@"PlaceCell" owner:self options:nil];
NSEnumerator *nibEnumerator = [nibContents objectEnumerator];
PlaceViewCell* placeCell = nil;
NSObject* nibItem = nil;
while ( (nibItem = [nibEnumerator nextObject]) != nil) {
if ( [nibItem isKindOfClass: [PlaceViewCell class]]) {
placeCell = (PlaceViewCell*) nibItem;
if ([placeCell.reuseIdentifier isEqualToString: @"Place" ]) {
//NSLog(@"PlaceCell - we have a winner!");
break; // we have a winner
} else {
placeCell = nil;
NSLog(@"PlaceCell is nil!");
}
}
}
return placeCell;
}
Then you can create the UITableViewCell subclass directly in Interface Builder.
IMO you aren't allowed to change the width of a UITableViewCell, but fortunately you don't need to. Apple supplies four default styles which are able to show one image at the left border of the cell (if you want to place it elsewhere you have to create a custom layout).
The four styles of UITableViewCellStyle are:
- UITableViewCellStyleDefault
- UITableViewCellStyleSubtitle
- UITableViewCellStyleValue1
- UITableViewCellStyleValue2
To embed an image into a UITableViewCell you should read the TableView Programming Guide at Apples Developer Connection network: Using Cell Objects in Predefined Styles
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"MyIdentifier"];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
NSDictionary *item = (NSDictionary *)[self.content objectAtIndex:indexPath.row];
cell.textLabel.text = [item objectForKey:@"mainTitleKey"];
cell.detailTextLabel.text = [item objectForKey:@"secondaryTitleKey"];
NSString *path = [[NSBundle mainBundle] pathForResource:[item objectForKey:@"imageKey"] ofType:@"png"];
UIImage *theImage = [UIImage imageWithContentsOfFile:path];
cell.imageView.image = theImage;
return cell;
}