Constraints between a UITableView and a UITableViewCell

Hi,


I'd like the columns of my UITableView to be aligned to the content of my UITableViewCell:


E.g UILabel for date column aligned to date cell label and so on.


Is there a way to create constraints between the two ?

What do you mean by columns? Do you mean the text in the section headers? If that's what you mean you could make your own custom section headers that positioned their content a certain way and then make your cells do the same.


Anyway, I don't think you can create constraints from anything inside a cell to anything outside the cell, at least I don't think it would work.

I have made constraints from a cell's subviews to things outside the cell, and it mostly worked (had a few issues with rotation that I worked around). But I didn't use it to position the contents of the cell; only to position something else not part of the table view such that it pointed to the cell. You could try adding constraints to position the cell contents to something in the section header but it does sound risky / fragile. Manual programmatic layout might be needed, or ditch the table view entirely and just use a UIScrollView or something.

But I didn't use it to position the contents of the cell; only to position something else not part of the table view such that it pointed to the cell.


>>> Yes, this is what I meant by columns. My header labels are not part of the table view, they are positioned above. And I want to position them relative to the content of the cell, which uses Auto Layout to adapt to different screen sizes. So, my question is "Can I create programmatically NSLayoutConstraint instances between these labels and some content in my cell ?". I can't believe no one else has the same problem...

In that case yes I believe you could. It would be a little messy since you'd have to add the constraint for horizontal position of your header label to every single cell that is displayed (or have some complicated bookkeeping to keep track of "which cell" the constraint was relative to). But it should be possible. You'd probably manage the constraints in willDisplayCell / didEndDisplayingCell.


In my case the only issue I had was during rotation animations - the external view wasn't being repositioned properly so I had to do a separate animation after the rotation which didn't look quite right. Not a huge deal and it may have been improved in recent iOS versions - I don't know. To be honest I haven't checked that issue recently.

I would recommend trying to use the same relationships to position your column headers and the views in the cells. Have a view that is the same width as the tableview. The column headers would be subviews of that view. The positions of the column header views would be relative to the container view. Set up the cells to use the same relationships relative to the cell content view.

Constraints between a UITableView and a UITableViewCell
 
 
Q