indentationLevelForRowAtIndexPath not indenting custom cell

前端 未结 9 1788
陌清茗
陌清茗 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.

    0 讨论(0)
  • 2020-12-01 13:08

    Have you accidentally overridden shouldIndentWhileEditing: to NO in your custom table cell class?

    0 讨论(0)
  • 2020-12-01 13:09

    If you use a custom cell with constraints, the most convenient way is to set up a constraint that would shift all content from the left edge, and then update it according to the indentation level in your cell subclass.

    Assuming indentationWidth is already set for the cell:

    override var indentationLevel: Int {
        didSet {
            self.leftConstraint.constant = CGFloat(self.indentationLevel) * self.indentationWidth
        }
    }
    
    0 讨论(0)
  • 2020-12-01 13:09

    The accepted answer could lead to an infinite loop on iOS8 in some cases. So it's better just to override setFrame of your custom UITableViewCell and adjust frame there.

    -(void)setFrame:(CGRect)frame {
        float inset = self.indentationLevel * self.indentationWidth;
    
        frame.origin.x += inset;
        frame.size.width -= inset;
    
        [ super setFrame:frame ];
    }
    
    0 讨论(0)
  • 2020-12-01 13:11

    Have you added the subviews of your ProjectItemTableViewCell to the cell's contentView? Also, you need to set the subviews' autoresizing masks so that they are repositioned when the contentView size changes.

    0 讨论(0)
  • 2020-12-01 13:12

    Constrain to the leading edge of the content view of the custom table cell you have in interface builder. For a simple margin, this seems to suffice. If you need to change the indentation programmatically, see Dean's answer. Or https://stackoverflow.com/a/7321461/5000071.

    0 讨论(0)
提交回复
热议问题