Ioannis Ioannis - 6 months ago 14
Swift Question

Cannot show Table View data in a simple Times Table App

I am trying to make a simple Times Table App (for numbers 1-9) in Swift) using a slider and a Table View. I am managing to make the slider work and an array to be created for each number that is selected with the slider and although the array is shown on the console. I cannot get the numbers to appear on the Table View. Can you please help me and tell me what am I missing?

Here is what I have written so far:

class ViewController: UIViewController, UITableViewDelegate {

@IBOutlet var sliderValue: UISlider!

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 9
}

@IBAction func sliderMoved(sender: UISlider) {
sender.setValue(Float(lroundf(sliderValue.value)), animated: true)

print(sliderValue)

var cellContent = [String]()

for var i = 1; i <= 10; i += 1 {
cellContent.append(String(i * Int(sliderValue.value)))
}

print(cellContent)

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")

cell.textLabel?.text = cellContent[indexPath.row]

return cell
}
}

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}

Answer

I'm afraid there's quite a lot in the code you've supplied that doesn't make all that much sense. I've mentioned some of it in my comment above but you've also nested what looks like a tableViewDataSource-function into your sliderMoved function. The whole array thing looks rather flakey as well as the proposed cell-count does not actually consider the size of the array. I think you probably want something like this:

class ViewController: UIViewController, UITableViewDataSource {
    @IBOutlet var valueSlider: UISlider!
    @IBOutlet var tableView: UITableView!
    private var cellContent = [String]()

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.dataSource = self
    }

    @IBAction func sliderMoved(sender: UISlider) {
        sender.setValue(Float(lroundf(valueSlider.value)), animated: true)
        tableView.reloadData()
    }

    // TableViewDataSource

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 9
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell") // Must exist with the same identifier in your storyboard
        cell.textLabel?.text = valueStringForIndex(indexPath.row)

        return cell
    }

    // Private functions

    private func valueStringForIndex(index: Int) -> String {
        return "\(index * Int(valueSlider.value))"
    }
}