views:

4833

answers:

2

Hi all,

Note: I'm using iPhone SDK 3.0 Final Version for Leopard.

I need to prepare a table listing similar to the one used by iPhone's Photo Album application. There are two new styles for UITableViewCell:

  1. UITableViewCellStyleValue1
  2. UITableViewCellStyleValue2

I thought i can use these instead of creating a custom cell class, but looks like they don't work properly. I'm facing following issues with UITableViewCellStyleValue2:

  1. I'm unable to show an image.
  2. If i change the font size of textLabel and detailTextLabel, the values are not aligned.

Here's the sample code:

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier] autorelease];
    }

    // Configure the cell.
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.imageView.image = [UIImage imageNamed:@"Pointy.gif"];

    cell.textLabel.text = @"Label";
    cell.textLabel.textColor = [UIColor redColor];
    cell.textLabel.font = [UIFont systemFontOfSize:21];

    cell.detailTextLabel.text = @"(10)";
    cell.detailTextLabel.textColor = [UIColor grayColor];
    cell.detailTextLabel.font = [UIFont systemFontOfSize:17];

    return cell;
}

This piece of code work with UITableViewCellStyleDefault and UITableViewCellStyleSubtitle to show images. Does UITableViewCellStyleValue2 support cell image property? I see blank space in place of image area (left corner of the cell). Am i doing something wrong here?

In case this doesn't work, how can i build a custom cell class with a style similar to UITableViewCellStyleValue2 style. I'm particularly interested in getting the detailTextLabel left aligned with textLabel.

Thanking in anticipation.

+2  A: 

The new builtin styles layout sets of fields with configurations that are the same as a number of the system layouts (like the cells in the AddressBook).

As to your exact specific:

  1. UITableViewCellStyleValue2 doesn't have an image
  2. The builtin styles layout logic assume the font metrics are what they are set to. If you change those the fields won't line up

You can get a detailed decription of what is available from each of the builtin styles in the documentation. If none of those set your needs you need to subclass UITableViewCell, add your own subviews, and layout them out in layoutSubviews. Apple has A large number of examples of how to the TableViewSuite.

Louis Gerbarg
Thank you for your reply. I'm familiar with TableViewSuit and have worked with custom TableViewCell. The results i'm looking to achieve are like UITableViewCellStyleValue2, with image. TableViewSuite examples doesn't address any situation with the detail Label is aligned "left" with the main Label. e.g. Photo Album application, where number of images in an album are written right next to the name of that album ... "Saved Photos (102)" where "(102)" is grey.
Mustafa
I don't understand what the issue is. If you are implementing a custom subview you can put whaetever subviews you want wherever you want. Add an ivar and a property for something named "leftAlignedImage" or similiar and set its frame to be 0,0,cellheight,cellhieight) in layoutSubviews.
Louis Gerbarg
@Louis: placing image and text, and detail text in some defined location is not a problem, placing them at a calculated position for each cell is the issue. The results I'm looking for are like in Photo Album application. I've posted a possible solution with a problem : )
Mustafa
A: 
Mustafa