indentationLevelForRowAtIndexPath not indenting custom cell

前端 未结 9 1789
陌清茗
陌清茗 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:24

    I used this code to indent my tableView cell. Initially it worked well, but later on this caused some problem (for example, interfering with my UITextView dynamic height update when indented, which is a subview of my tableView cell. Somehow my UITextView think it's width still is the original contentView's width).

    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
    )
    

    So I don't recommend the code above, instead I use the auto layout method as Dean's answer with a little difference (Swift version):

    override func awakeFromNib() {
        super.awakeFromNib()
        self.indentationWidth = 20.0
        self.indentationLevel = 0
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        let margin: CGFloat = 20.0
        let indentPoints = CGFloat(self.indentationLevel) * self.indentationWidth
        indentConstraint.constant = margin + indentPoints
    }
    

    The "indentConstraint" is a IBOutlet (leading constraint with the contentView), and the code is written in a custom tableViewCell subclass, it worked perfectly, and you don't need to remove or add any constraints, which is more expensive. I think the auto layout method is the better way to indent tableView cell.

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

    Yeah, it seems like custom table cells don't do this automatically? You need to override the layoutSubviews method in the table cell class. See this question for how to do this.

    This code worked perfectly for me (although be careful if you are setting a custom height w/ the delegate as well, they seem to interfere with each other):

    - (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
        );
    }
    

    Edit for iOS8 and later

    The above does work for me on iOS, but it causes subtle bugs when trying to autosize the height of the cell as well. There is n easier solution: If you have autolayout turned for the cell just set the left margin of the contentView:

    override func layoutSubviews() {
        super.layoutSubviews()
        self.contentView.layoutMargins.left = CGFloat(self.indentationLevel) * self.indentationWidth
        self.contentView.layoutIfNeeded()
    }
    
    0 讨论(0)
  • 2020-12-01 13:27

    The indentation level is a property of the UITableViewCell itself. Try setting it on the cell when you create it, and return this value in tableView:indentationLevelForRowAtIndexPath:

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