Saturday, June 8, 2013

UITableViewCell indentationWidth example in Objective C (iOS).


UITableViewCell indentationWidth

The width for each level of indentation of a cell'€™s content.

@property(nonatomic) CGFloat indentationWidth

Discussion of [UITableViewCell indentationWidth]
The default indentation width is 10.0 points.

UITableViewCell indentationWidth example.
- (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
    );
}

Example of [UITableViewCell indentationWidth].
// initialize cell if need be
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
  cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}

// setup the accessory
UIImage *indicatorImage = [UIImage imageNamed:@"indicator.png"];
cell.accessoryView = [[[UIImageView alloc] initWithImage:indicatorImage] autorelease];

// TODO: rewrite this
UIImage *image = [UIImage imageNamed:@"64x64.png"];
const CGFloat LABEL_HEIGHT = 25;
CGFloat x = image.size.width + 2.0 * cell.indentationWidth;
CGFloat y = 0.8 * (tableView.rowHeight - 1.7 * LABEL_HEIGHT);
CGFloat w = tableView.bounds.size.width - image.size.width - 4.0 * cell.indentationWidth - indicatorImage.size.width;
CGFloat h = LABEL_HEIGHT;
CGRect labelFrame = CGRectMake(x,y,w,h);

// build a label and use it as the content view
UILabel* topLabel = [[[UILabel alloc] initWithFrame:labelFrame] autorelease];
topLabel.textColor = [UIColor colorWithRed:0.25 green:0.0 blue:0.0 alpha:1.0];
topLabel.highlightedTextColor = [UIColor colorWithRed:1.0 green:1.0 blue:0.9 alpha:1.0];
topLabel.font = [UIFont systemFontOfSize:20];
topLabel.textAlignment = UITextAlignmentCenter;
topLabel.text = [NSString stringWithFormat:[aboutArray objectAtIndex:[indexPath row]]];
topLabel.textAlignment = UITextAlignmentCenter;

[cell.contentView addSubview:topLabel];

UITableViewCell indentationWidth example.
- (void)layoutSubviews
{
    [super layoutSubviews];

    self.contentView.frame = CGRectMake(0,                                         
                                        self.contentView.frame.origin.y,
                                        self.contentView.frame.size.width,
                                        self.contentView.frame.size.height);

    if (self.editing
        && ((state & UITableViewCellStateShowingEditControlMask)
        && !(state & UITableViewCellStateShowingDeleteConfirmationMask)) ||
            ((state & UITableViewCellStateShowingEditControlMask)
         && (state & UITableViewCellStateShowingDeleteConfirmationMask)))
    {
        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);   
    }
}

End of UITableViewCell indentationWidth example article.