2017-08-17 76 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 = []重新设置数组滑块,现在它工作正常