James Dunay
James Dunay

Reputation: 2724

How to indent custom uilabel in a UITableViewCell when editing

I have a custom uitableviewcell that i would like to indent when I turn on this:

[self.boatsDisplay setEditing:YES animated:YES];

Could anyone provide me a hint or some guidance?

Thanks

Upvotes: 1

Views: 3813

Answers (2)

Matthias
Matthias

Reputation: 163

In your UITableViewDelegate you can use the tableView:indentationLevelForRowAtIndexPath: method:

- (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath {
  if(tableView.editing == YES){
    return 1; // or higher integer
  } else {
    return 0;
  }
}

if you want to check for your custom cell only you can add &&[[tableView cellForRowAtIndexPath:indexPath] isKindOfClass:yourTableViewCell] in the if condition.

Upvotes: 2

Mark Adams
Mark Adams

Reputation: 30846

You'll need to subclass UITableViewCell and override -layoutSubviews. When the cell's editing bit is set to YES, -layoutSubviews will automatically be invoked. Any changes made within -layoutSubviews are automatically animated.

Consider this example

- (void)layoutSubviews
{
    [super layoutSubviews];
    CGFloat xPosition = 20.0f; // Default text position

    if (self.editing)
        xPosition = 40.0f;

    CGRect textLabelFrame = self.textLabel.frame;
    textLabelFrame.origin.x = xPosition;
    self.textLabel.frame = textLabelFrame;
}

Upvotes: 4

Related Questions