views:

1860

answers:

2

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.

+1  A: 

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.

frankodwyer
Mind pointing me in the direction of these useful APIs?I looked at the 2.2/3.0 diffs doc and could not find the new style options that you are talking about.Also, does it help to create the custom view cell in Interface Builder for some reason? I'm not having trouble creating the cell, I'm just having trouble adjusting its width.
see answer from Daskaja for details of the predefined cell view styles in 3.0
frankodwyer
+1  A: 

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;
}
DASKAjA
Thanks for sharing those, but they don't appear to be useful for what I'm trying to accomplish. I would like the picture to be outside of the cell altogether (offset just to the left of the cell) as the picture in the post that I linked shows. Again, I do not want the image inside of the cell. Open Contacts on your iPhone or iPod touch and go into a person's details. You can see exactly what I am trying to accomplish there. There is an image offset to the left of cells at the top.
Ok I see, but you should really read that tableview programming guide. Among the explained ways here there's another way to go. If you entirely subclass UITableViewCell you're completely free in what you want to render.
DASKAjA