Why does my UITableView formatting go completely crazy when I return from the view controller I went to?

I have one UITableView

with a custom cell that has multiple labels that dynamically determine the height of the cell. When I click on one cell and navigate to a new view controller, after returning, all the formatting for the cells is completely messed up and I can't figure out what's causing it.

This is how cells usually look:

enter image description here

And I have some basic limits set on them. The top label is attached to the top and left edges and should always be> = 20 on the right. The other marks are aligned to the left of this first mark, with vertical spacing between them. The middle mark has a right-hand distance constraint to the field, and the bottom marks are aligned with the baseline of the first and have a horizontal distance between them.

When I go back to this table, it looks like this:

enter image description here

I can't figure out what is causing his disposition any differently than when I left. If I scroll around it seems to "reset" them back to what they should be, but on boot they really messed up. I can attach a project if I wish, but there really isn't much outside of the storyboard.

cellForRowAtIndexPath:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as CustomTableViewCell

    let object = objects[indexPath.row]

    cell.title1.text = object.name
    cell.title2.text = object.color
    cell.title3.text = object.roar

    return cell
}

      

Sample project: http://cl.ly/040L2z0q0V2d

+3


source to share


5 answers


It looks like the table cells of the table do not change depending on the content when returning from the session. Using the sample project, I threw the reload data into viewWillAppear and seems to have solved the problem.



override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
    self.tableView.reloadData()
}

      

+3


source


There are several issues with your project.

Data loading and Autostart .

The first one causes strange behavior when drawing data cells. When you disconnect from the segue, you will see these extra cells on top of the table, caused by an ambiguous layout calculation.

Solution . Move the data in override func viewWillAppear(animated: Bool) {

and execute tableView.reloadData()

(as @rFessler correctly suggested).

On the other hand, Autolayout is a kind of fiery beast. Tamable. It is worth investigating this topic further. I haven't been able to set up your layout with cell height automation, but I'll leave a few links and a project for you.




Literature:

http://www.appcoda.com/self-sizing-cells/

http://captechconsulting.com/blog/tyler-tillage/ios-8-tutorial-series-auto-sizing-table-cells




Project: http://cl.ly/3z3a2Z3a3U2K

+2


source


I played around with this and found a simple solution adding that this seems to fix the problem.

override func viewWillDisappear(animated:Bool) {
    super.viewWillDisappear(animated)
    self.tableView.estimatedRowHeight = 166.0
}

      

+1


source


I had a similar problem. I uploaded your project and it looks like I decided to fix it and tweak some limitations. This is what my constraints look like:

enter image description here

Also I added this to viewDidLoad:

self.tableView.estimatedRowHeight = 120
self.tableView.rowHeight = UITableViewAutomaticDimension

      

I also added this to check for delete:

override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
    if editingStyle == .Delete
    {
        self.objects.removeAtIndex(indexPath.row)

        self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
    }
}

      

Now you can even rotate the device and delete lines, and it's great!


However, if you push this view to the navigation controller (which is what my problem was about in the beginning), it's still a problem. Check out my storyboard below for some funky shortcuts:

enter image description here

To fix this we seem to need to do a hack! (Damn you apple, what's going on with that ?!)

var firstAppearance=true
override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
    if firstAppearance
    {
        if let indexPaths = self.tableView.indexPathsForVisibleRows()
        {
            self.tableView.reloadRowsAtIndexPaths(indexPaths, withRowAnimation: UITableViewRowAnimation.None)
            self.firstAppearance = false
        }
    }
}

      

At this point, I think this is as good as it gets.

+1


source


Since the method tableView:estimatedHeightForRowAtIndexPath

will be called every time you change to a new MVC and change autostart, you can simply do

override func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return UITableViewAutomaticDimension
}

      

to reuse auto power off

0


source







All Articles