How to update auto layout of UItableViewCell in UItableViewController when height or width changes in Swift?

I am working on a Swift project where I am using UITableView. I populate my Tableview using delegate methods:

  • override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int

  • override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell

  • dequeueReusableCellWithReuseIdentifier

  • etc.,

and not use a collectionView.reloadData()

when my model changes (add / remove element).

My cells inside my TableView can have different sizes in the function for the number of items inside my model. lie that:

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize{
    var size = CGSize(width: 0,height: 0)

    if TimerManager.sharedInstance.timers.count < 2 {
        size = collectionView.frame.size
    } else if TimerManager.sharedInstance.timers.count == 2 {
        size = CGSize(width: collectionView.frame.size.width,height: collectionView.frame.size.height/2)
    } else if  TimerManager.sharedInstance.timers.count == 3 {
        if indexPath.row < 2 {
            size = CGSize(width: collectionView.frame.size.width/2,height: collectionView.frame.size.height/2)

        } else {
            size = CGSize(width: collectionView.frame.size.width,height: collectionView.frame.size.height/2)
        }

    } else {
        size = CGSize(width: collectionView.frame.size.width/2,height: collectionView.frame.size.height/2)
    }

    return size
}

      

But my problem: the new cells have good layout syntax, but the old cells were not updated. Moreover, my model always changes when the table view is not displayed .

This is what happened on the screen: Screens

I am trying different ways to force update, please:

  • setNeedDisplay

    a foreach cell to force repetition of the drawing (and override the draw to check if it's caused)
  • reloaddata when on viewWillAppear

  • setNeedsUpdateConstraints

    , setNeedsLayout

    on the cell

But nothing solves my problem!

A friend who is a pure iOS developer at work will tell me that there is some issue on iOS 8 with auto-layout. Perhaps this is related?

I hope someone might have some ideas or solutions here :)

(I hope my English is not too bad to understand me, it is not my native language)

+3


source to share





All Articles