Neko
Neko

Reputation: 589

(iOS) preferredLayoutAttributesFitting method did not invoke

I use preferredLayoutAttributesFitting to dynamic my cell frame. but which confused me is , the method preferredLayoutAttributesFitting is not invoked at first. I have to reload my uicollectionView to invoke it.

my code:

    override func viewDidLoad() {
    super.viewDidLoad()

self.recommendDatas.removeAll()

    let para = RecommendGoodsReqModel()
    para.user_id = XLD_USER_ID()

    Swift_API.REQUEST_MallRecommendData(parameter: para) { (resModel) in

        for dataModel in resModel.datas!{

            self.recommendDatas.append(dataModel)

        }

        self.collectionView.mj_header.endRefreshing()
        self.collectionView.reloadData()<-first time reloadData

    }

}

and the collectionView gave me nothing . the whole page is blank.(I have an break point in preferredLayoutAttributesFitting , not invoke) and I have method - pull down to reload :

    override func headerRefrshAction(){

    Swift_API.REQUEST_MallRecommendData(parameter: para) { (resModel) in

    for dataModel in resModel.datas!{

        self.recommendDatas.append(dataModel)

    }

    self.collectionView.mj_header.endRefreshing()
    self.collectionView.reloadData()<-second time to reloadData

}

totally the same code. but this time , preferredLayoutAttributesFitting had been invoked and the page shows normally . so I don't know why the method preferredLayoutAttributesFitting not called at first time I reload my collectionView.

any one knows it?

Upvotes: 1

Views: 3553

Answers (1)

Rand
Rand

Reputation: 195

I was having the opposite problem, preferredLayoutAttributesFitting was called on the first load/layout of my collection view (and my cells were auto sizing correctly), but it was not called on subsequent reload/layouts (and the cells reverted to their estimated size).

The fix for me was to invalidate the layout before reloading the collection view

        self.collectionView.collectionViewLayout.invalidateLayout()
        self.collectionView.reloadData()

Upvotes: 1

Related Questions