2

The table does not display the updated array to return to the cell. When I launch the app I get blank cells. All permissions have been given in the storyboard. I tried the tableView.reloadData() everywhere but can't seem to make it work either. If anyone can explain where Im going wrong that would really help me get better.

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)
    }
}
9
  • 4
    Why would you call tableView.reloadData() inside cellForRowAt? That puts you in in infinite loop, since it will again call cellForRowAt... You only need to call tableView.reloadData() if you change the data source after your table view was set up. Commented Aug 17, 2017 at 13:34
  • the data gets updated when the slider moves. wrong ? Commented Aug 17, 2017 at 13:36
  • Yes, so you should call tableView.reloadData() from inside sliderSelector or rather do it from inside tableGenerate()... @Dev0urCode check my answer. Commented Aug 17, 2017 at 13:37
  • You only have to populate arrayTable and call tableView.reloadData() once. That will get the number of rows from numberOfRowsInSection and will ask you for the initialized cell (in cellForRowAt). So cellForRowAt should return the UITableViewCell only, and do nothing more. Commented Aug 17, 2017 at 13:44
  • @DávidPásztor if I put tableview.reloaddata in the slider section I get Ambiguous reference to member 'tableView(_:numberOfRowsInSection:)' Commented Aug 17, 2017 at 13:50

3 Answers 3

3

Add outlet of tableview like this and connect with table in storyboard-

@IBOutlet weak var tableView1: UITableView!

Change code to this -

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()

}
Sign up to request clarification or add additional context in comments.

5 Comments

if I do that i get Ambiguous reference to member 'tableView(_:numberOfRowsInSection:)' at tableView.reloadData line
than that gives me Use of unresolved identifier 'tableView1'
You are right; progress !! I forgot to create the outlet ! I created @IBOutlet weak var table: UITableView! and it works by updating on the first move of the slider to the initial slider value but when I move the slider further it does not update anymore.
using table.reloadData()
I found the issue, forgot to add arrayTable = [] in my sliderslector so I thought the table was not updating when in fact it was just getting bigger. Thanks a million for your help !
2

By calling tableView.reloadData() inside cellForRowAt, you create an infinite loop, since reloadData() automatically calls cellForRowAt. You need to move reloadData() inside tableGenerate(), since it should only be called when your data source is changed.

You also need to create an IBOutlet for your tableView in Storyboard. Since you are not using a UITableViewController, you need to do this manually.

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()
    }
}

Comments

-1

forgot to create the table outlet ! I also forgot to reset the array with the array slider with arrayTable = [] now it works fine

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.