Preload cells of uitableview

前端 未结 5 1117
逝去的感伤
逝去的感伤 2021-02-20 15:10

I acknowledge that UITableview load dynamically a cell when user scrolls. I wonder if there is a way to preload all cells in order not to load each one while scrolling. I need t

5条回答
  •  醉话见心
    2021-02-20 15:57

    As suggested by Mark I also changed the height of my UITableView temporarily so that the table view creates enough reusable cells. Then I reset the height of my table view so that it stops creating reusable cells while scrolling.

    To accomplish that I create a helper bool which is set to false by default:

    var didPreloadCells = false
    

    It is set to true when my table view first reloaded data and therefore created the first reusable cells.

    resultsHandler.doSearch { (resultDict, error) -> Void in
        [...]
    
        self.tableView.reloadData()
    
        self.didPreloadCells = true
    
        [...]
    }
    

    The real trick happens in my viewDidLayoutSubviews Method. Here I set the frame of my table view depending on my boolean. If the reusable cells were not created yet I increase the frame of the table view. In the other case I set the normal frame

    override func viewDidLayoutSubviews() {
    
        super.viewDidLayoutSubviews()
    
        self.tableView.frame = self.view.bounds
    
        if !didPreloadCells
        {
            self.tableView.frame.size.height += ResultCellHeight
        }
    }
    

    With the help of that the table view creates more initial reusable cells than normal and the scrolling is smooth and fluent because no additional cells need to be created.

提交回复
热议问题