indentationLevelForRowAtIndexPath not indenting custom cell

前端 未结 9 1802
陌清茗
陌清茗 2020-12-01 12:51

I have overridden the tableView:indentationLevelForRowAtIndexPath method in my UITableViewController derived class as follows:

- (N         


        
9条回答
  •  一个人的身影
    2020-12-01 13:06

    Similar to the accepted answer, this is how it can be done in iOS 8 while still using layoutSubviews with AutoLayout instead.

    With _viewConstraints as an NSMutableArray ivar and _imageView as the closest view to the left side of the cell's content view.

    - (void)layoutSubviews
    {
        float indentPoints = indentationLevel * [self indentationWidth];
    
        [self removeConstraints:_viewConstraints];
        [_viewConstraints removeAllObjects];
    
        NSDictionary *views = NSDictionaryOfVariableBindings(imageView);
        [_viewConstraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"H:|-(%f@1000)-[_imageView]", indentPoints] options:0 metrics:nil views:views]];
        [self addConstraints:_viewConstraints];
    }
    

    If you have AutoLayout constraints defining everything else in the view then this should push the whole view over the desired indentation amount.

    NOTE if using Nib: You should define the constraint (in this case between _imageView and its super view) as >= some number (in my case it was 20). Then the original constraint, and the one being added/removed in layoutSubviews don't conflict with each other.

    You should also consider calling the following in awakeFromNib

    [_imageView setTranslatesAutoresizingMaskIntoConstraints:NO]
    

    This is so the old "springs and struts" don't get in the way of the constraints you are adding.

提交回复
热议问题