views:

195

answers:

1

I have created a UITableViewCell derived class which resizes itself to display at an indentation level. I have tried setting indentationLevel and indentationWidth using the following in the ViewControllers cellForRowAtIndexPath:

cell.indentationLevel = [[item objectForKey:@"indent"] intValue];
cell.indentationWidth = CELL_INDENT_SIZE;

This in itself does not indent the cell, so I am using the following layoutSubviews in the UITableViewCell:

-(void)layoutSubviews {
    CGFloat indentSize = (self.indentationLevel - 1) * self.indentationWidth;
    CGRect r = containerView.frame;
    containerView.frame = CGRectMake (r.origin.x + indentSize, r.origin.y, r.size.width - indentSize, r.size.height);
    DLog(@"frameRect : %f, %f, %f, %f, %f",indentSize, r.origin.x, r.origin.y, r.size.width, r.size.height);
    [super layoutSubviews];
}

This works fine for the initial layout, but when the cell is selected, it recalculates itself from the new dimensions and effectively shrinks the cell every time it is selected.

How can I resize this cell so that it doesn't keep changing the layout.

A: 

layoutSubviews is called every time the cell has to redraw itself. I imagine its being called every time the cell has to draw itself highlighted.

In any case, you wouldn't want to resize the cell itself there. drawRect: is the place to that.

Even better yet, just use the built-in indentation system. It handles all this for you unless you're doing something really unusual.

TechZen