views:

87

answers:

2

I have subclassed UITableViewCell to create a custom cell with a button and 2 labels. The cell definition is loaded from a xib using the pattern outlined in Dave Mark's Beginning iPhone Development. Here's the essential code:

    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"MarketItemCustomCell" owner:self options:nil];

    for (id oneObject in nib)
    {
        if ([oneObject isKindOfClass:[MarketItemCustomCell class]])
        {
            cell = (MarketItemCustomCell *)oneObject;
            break;
        }
    }

The labels and button display as expected but the indentation level is not respected. I have implemented indentationLevelForRowAtIndexPath like below, but the cell is still aligned all the way to the left.

- (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        <snip/>
        return 5;
    }

Note that the indentation level works fine when I'm not using a custom cell.

Any hints?

A: 

I think that you need to customize the indentation level in your custom UITableViewCell. I guess this method (and methods like – tableView:heightForRowAtIndexPath:) will not affect in the case of UITableViewCell if your custom UITableViewCell already specified it

vodkhang
+1  A: 

Based on vodkhang's suggestion, I implemented the following solution in my UITableViewCell sublass. Not sure if this is the best solution but it appears to work fine.

- (void)layoutSubviews
{
    [super layoutSubviews];

    float indentPoints = self.indentationLevel * self.indentationWidth;

    self.contentView.frame = CGRectMake(indentPoints,
                                          self.contentView.frame.origin.y,
                                          self.contentView.frame.size.width - indentPoints, 
                                          self.contentView.frame.size.height);
}
Daniel
This works perfectly!
Nathan Reed