Mustafa
Mustafa

Reputation: 20564

iPhone Development - UITableViewCellStyleValue2

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.

Upvotes: 1

Views: 19999

Answers (1)

Louis Gerbarg
Louis Gerbarg

Reputation: 43452

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.

Upvotes: 2

Related Questions