Saturday, June 8, 2013

UITableViewCell indentationLevel example in Objective C (iOS).


UITableViewCell indentationLevel

Adjusts the indentation level of a cell whose content is indented.

@property(nonatomic) NSInteger indentationLevel

Discussion of [UITableViewCell indentationLevel]
The default value of the property is zero (no indentation). The width for each level of indentation is determined by the indentationWidth property.

UITableViewCell indentationLevel example.
- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
        if (cell == nil)
        {
            cell = (UITableViewCell*)[[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"cellType01"] autorelease];
            [cell setIndentationLevel:indexPath.row];
            [cell setIndentationWidth:10];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            // Tag the view
            [[[cell subviews] objectAtIndex:0] setTag:111];
            UILabel* labelView = [[UILabel alloc] initWithFrame: CGRectMake(cell.indentationLevel*cell.indentationWidth, 0, 300, 20)];
        }
   }

Example of [UITableViewCell indentationLevel].
- (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
    );
}

UITableViewCell indentationLevel 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 indentationLevel example article.