2017-08-17 74 views
2

該表不顯示更新的數組以返回到單元格。當我啓動應用程序時,我得到空白單元格。所有權限都在故事板中給出。我嘗試了tableView.reloadData()無處不在,但似乎無法使其工作。如果任何人都可以解釋我哪裏錯了,那真的會幫助我好起來。tableView.reloadData()表未更新

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { 

@IBOutlet weak var slider: UISlider! 

@IBAction func sliderSelector(_ sender: Any) { 
    tableGenerate() 
} 

var arrayTable = [Int]() 

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

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

    let cell = UITableViewCell(style:UITableViewCellStyle.default, reuseIdentifier: "Cell") 
    cell.textLabel?.text = String(arrayTable[indexPath.row]) 
    tableView.reloadData() 
    return cell 
} 

func tableGenerate() { 
    var tableMultiplier = 1 
    while tableMultiplier <= 50 { 
     arrayTable.append(tableMultiplier * Int(slider.value)) 
     tableMultiplier += 1 
     print(arrayTable) 
    } 
} 
+4

爲什麼要在'cellForRowAt'內調用'tableView.reloadData()'?這會讓你進入無限循環,因爲它會再次調用'cellForRowAt' ...如果在設置了表視圖後更改數據源,則只需調用'tableView.reloadData()'。 –

+0

數據在滑塊移動時得到更新。錯了? – Dev0urCode

+0

是的,所以你應該從'sliderSelector'內調用'tableView.reloadData()',或者在'tableGenerate()'... @ Dev0urCode檢查我的答案。 –

回答

2

通過調用tableView.reloadData()cellForRowAt,爲您創造一個無限循環,因爲reloadData()自動調用cellForRowAt。您需要將reloadData()移至tableGenerate()之內,因爲只有在您的數據源發生更改時才應調用它。

您還需要在Storyboard中爲您的tableView創建一個IBOutlet。由於您沒有使用UITableViewController,因此您需要手動執行此操作。

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { 

    @IBOutlet weak var tableView: UITableView! 
    @IBOutlet weak var slider: UISlider! 

    @IBAction func sliderSelector(_ sender: Any) { 
     tableGenerate() 
    } 

    var arrayTable = [Int]() 

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

    public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = UITableViewCell(style:UITableViewCellStyle.default, reuseIdentifier: "Cell") 
     cell.textLabel?.text = String(arrayTable[indexPath.row]) 
     return cell 
    } 

    func tableGenerate() { 
     var tableMultiplier = 1 
     while tableMultiplier <= 50 { 
      arrayTable.append(tableMultiplier * Int(slider.value)) 
      tableMultiplier += 1 
      print(arrayTable) 
     } 
     tableView.reloadData() 
    } 
} 
3

在storyboard-

@IBOutlet weak var tableView1: UITableView! 

更改密碼這個添加的tableview的出口是這樣,並連接有桌子 -

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { 

@IBOutlet weak var slider: UISlider! 
@IBOutlet weak var tableView1: UITableView! 

@IBAction func sliderSelector(_ sender: Any) { 
    tableGenerate() 
} 

var arrayTable = [Int]() 

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

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

    let cell = UITableViewCell(style:UITableViewCellStyle.default, reuseIdentifier: "Cell") 
    cell.textLabel?.text = String(arrayTable[indexPath.row]) 
    return cell 
} 

func tableGenerate() { 
    var tableMultiplier = 1 
    while tableMultiplier <= 50 { 
     arrayTable.append(tableMultiplier * Int(slider.value)) 
     tableMultiplier += 1 
    } 
     print(arrayTable) 
    tableView1.reloadData() 

} 
+0

如果我這樣做,我得到含糊不清引用成員' tableView(_:numberOfRowsInSection :)'在tableView.reloadData行 – Dev0urCode

+0

比那給了我使用未解決的標識符'tableView1' – Dev0urCode

+0

你是對的;進步!我忘了創造出口! 我創建了@IBOutlet weak var table:UITableView!它通過將滑塊的第一次移動更新爲初始滑塊值來工作,但是當我將滑塊進一步移動時,它不再更新。 – Dev0urCode

-1

忘了創建表格插座!我也忘了用arrayTable = []重新設置數組滑塊,現在它工作正常