2017-08-08 81 views
-3

目前我有一個tableview,當我向下滾動(滾動視圖)時加載單元格。是否有可能加載和填充viewDidLoad上的所有單元格。我想先將數據分配給單元格,然後才能查看。我試圖使用self.tableView.reloadData()但不成功。在ViewDidLoad之前/之前加載TableView單元格?

+0

即反對使用的UITableView,這是重複使用的細胞的推薦方法。如果由於某種原因而不想實現這種行爲,那麼可以嘗試使用靜態單元格UITableViews,或者甚至在每個單元格堆疊使用UIStackView – Koesh

回答

1

如果您不想使用UITableView's單元重用概念,則在viewDidLoad中事先創建所有UITableViewCells並將它們存儲在數組中。

實施例:

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] 
    } 
} 
相關問題