HaVaNa7
HaVaNa7

Reputation: 330

swift heightForRowAt indexPath not update

With this code I set automatic dimension of a cell based on the number of items in array. It works, but when I add some new item with another viewController and then return to this the height does'n update even if the number of array get increased.

I get the array in viewDidAppear

can someone help me?

  override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

        
      print("called")        
        
           let misure = self.array.count
 
            let cellHeight = CGFloat(50.0 * Float(misure))
            
            print(cellHeight)
            print(misure, " misure")

            let section = indexPath.section
            let row = indexPath.row
            if section == 3 && row == 0{
                return 100.0
            }else if section == 4 && row == 0{
                return cellHeight + 60.0
            }
            return 44.0
        
        
    }

in viewDidLoad I have set

tableView.estimatedRowHeight = 44

Upvotes: 1

Views: 1027

Answers (3)

perteadi
perteadi

Reputation: 13

I think if you call tableView.layoutIfNeeded() should be better. This will update any layout updates that are in a pending state. Didn't try to modify the height with this, but it should work.

Upvotes: 1

HaVaNa7
HaVaNa7

Reputation: 330

wow I found a solution!

Thanks to this answer: Swift: How to reload row height in UITableViewCell without reloading data

Just used

tableView.beginUpdates()
tableView.endUpdates()

in viewDidAppear

I hope that it will help someone else!

Upvotes: 0

Wasi Tariq
Wasi Tariq

Reputation: 21

call tableView.reloadData() after getting the array update

Upvotes: 0

Related Questions