简体   繁体   中英

UITableViewCell indentation works only for default cell textLabel

I have got custom UITableViewCell and not able set indentation in cellForRow: atIndexPath: .

cell.indentationWidth = 10.0
cell.indentationLevel = indexTuples.count //Dynamic

However, this works if iOS provided default cell label :

cell.textLabel?.text = "TEST"

Is there any restriction? How can I solve this ?

Note: I have to use Autolayout .

UPDATE :

As per comments, I referred here , I gave a try with below code which doesn't seems to be helpful.

override func layoutSubviews() {
    super.layoutSubviews()
    self.contentView.layoutMargins.left = CGFloat(self.indentationLevel) * self.indentationWidth
    self.contentView.layoutIfNeeded()
}

In short : With the help of Autolayout leading constraint, I am able to solve this problem.

First I created a UIView say containerView inside cell contentView & moved all the elements of cell contentView into containerView . Later I made IBOutlet of "constainerView leading constraint".

Then in the cellForRow: atIndexPath table view delegate updated its constant as :

cell.indentViewLeading.constant = 5 + CGFloat(10 * indexTuples.count - 1)  //indexTuples.count gives dynamic values
cell.indentationWidth = 10.0
cell.indentationLevel = indexTuples.count

In my UITableViewCell subclass :

@IBOutlet weak var indentViewLeading: NSLayoutConstraint!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM