-3

Currently I have a tableview that loads cells when I scroll down (scrollview). Is it possible to load and populate all cells on viewDidLoad. I would like to assign data to a cell before it can be viewed. I have tried using self.tableView.reloadData() but not successful.

P. Kwong
  • 61
  • 2
  • 9
  • That is against the recommended way of using UITableView, which is reusing cells. If you wan't to achieve this behaviour for some reason, you could try with static cells UITableViews or even with an UIStackView with every cell stacked – Koesh Aug 08 '17 at 06:58

1 Answers1

1

If you don't want to use UITableView's cell reusing concept, then create all the UITableViewCells beforehand in viewDidLoad and store them in an array.

Example:

class ViewController: UIViewController, UITableViewDataSource
{
    var arr = [UITableViewCell]()

    override func viewDidLoad()
    {
        super.viewDidLoad()
        //Create your custom cells here and add them to array
        let cell1 = UITableViewCell()
        let cell2 = UITableViewCell()
        arr.append(cell1)
        arr.append(cell2)
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        return arr.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
        return arr[indexPath.row]
    }
}
PGDev
  • 23,751
  • 6
  • 34
  • 88