Saturday, June 8, 2013

UITableViewCell setIndentationLevel example in Objective C (iOS).


UITableViewCell setIndentationLevel

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

@property(nonatomic) CGFloat indentationWidth

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

UITableViewCell setIndentationLevel 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 setIndentationLevel].
self.switchView = [[UISwitch alloc] initWithFrame:CGRectMake(7, 8, 79, 27)];
[switchView addTarget:self action:@selector(setItem:) forControlEvents:UIControlEventValueChanged];
[switchView setTag:9001];
[cell addSubview:switchView];
[cell setIndentationWidth:45];
[cell setIndentationLevel:1];

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