Custom TableView Overrides Table View Cells

I designed a custom view as a UITableView header view. exactly so (I just put a link to an image instead of an image, since I don't have 10 reputations.)

http://i.stack.imgur.com/KhNbE.png

Then in my UITableViewController I use this view as tableHeaderView

 override func viewDidLoad() { tableView.tableHeaderView = headerView! //...other things } 

I got text from JSON to execute ContentLabel . If the text is long, the View header will overlap the cells as shown below. (Short text is ok)

http://i.stack.imgur.com/gtO2g.png

The section is visible, but the two rows of the cell overlap with the headerView. I'm not sure that I made the wrong restrictions or code on ContentLabel . Below is the code in which I configured ContentLabel in TopicHeaderView.swift

 var content: String? { didSet { self.contentLabel.text = content! self.setNeedsUpdateConstraints() self.updateConstraintsIfNeeded() self.setNeedsLayout() self.layoutIfNeeded() } } func setFrameHeight(height: CGFloat) { var frame = self.frame frame.size.height = height self.frame = frame } override func layoutSubviews() { super.layoutSubviews() self.contentLabel.preferredMaxLayoutWidth = self.contentLabel.alignmentRectForFrame(contentLabel.frame).width self.titleLabel.preferredMaxLayoutWidth = self.titleLabel.bounds.size.width self.authorLabel.preferredMaxLayoutWidth = self.authorLabel.bounds.size.width self.setFrameHeight(CGRectGetMaxY(contentLabel.frame) + 8) } 

I looked at similar questions in SO, but it seems I can not find a solution to solve my problem. Can anyone help with this?

Edition:

I registered the original CGPoint of my first tableView table and the height of the header. It shows the correct number, which means that the first cell is right next to the vertical view of the header. There is a difference of 22 points due to the height of the course section.

 headerheight:600.0 first cell y: 622.0 

So maybe the problem with the label is that its height is too large to exceed the bounds of the TableView headerView? I'm not sure.

Edition:

Strange things happen. I registered the value of y headerView bottom, ContentLabel bottom and first UITableViewCell origin. Please see the image from the link in the comment to the question below (still need 10 reputation)

As you can see, from the value in the console, the sequence of views at the top should be "contentLabel bottom (value: 224) - the lower bounds of the header (value: 232) - the beginning of the first cell (value: 254)" . But in the simulator, the sequence is completely confused. It turns the "lower bounds of the headerView - the beginning of the first cell - contentLabel bottom"

I really appreciate if anyone can help with this.

+6
source share
2 answers

The problem is that the UITableView does not automatically change the position of the cells when the height of the headerView . Thus, you need to reload the UITableView every time TopicHeaderView.content changes.

+3
source

Select this title view or image. Look at what you have and check the Clip Subviews tab in the Attributes Inspector .

It worked for me. Before

Storyboard

-1
source

Source: https://habr.com/ru/post/985853/


All Articles