1

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()
}
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
byJeevan
  • 3,728
  • 3
  • 37
  • 60
  • Possible duplicate of [indentationLevelForRowAtIndexPath not indenting custom cell](https://stackoverflow.com/questions/2502822/indentationlevelforrowatindexpath-not-indenting-custom-cell) – Sandeep Bhandari Nov 16 '17 at 07:51
  • @Sandeep Bhandari Thanks for sharing link. However overriding layoutSubviews of cell doesn't helped me. But a simple autolayout trick solved my problem. ;-) – byJeevan Nov 16 '17 at 09:30
  • 1
    Am glad u solved it :) – Sandeep Bhandari Nov 16 '17 at 09:34

1 Answers1

0

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!
byJeevan
  • 3,728
  • 3
  • 37
  • 60
  • Why do you still need cell.indentationWidth =10.0 and cell.indentationLevel = indexTuples.count? Isn't adjusting your leading constraint enough? If apple enables indentation for your custom cell indentation is going to double..... – Philip De Vries May 20 '19 at 13:22