I am newbie in Swift and trying to show multiplication table in UITableView and changing the data based on the slider value. The model is updated on changing the slider position but the tableView is not showing the update for currently visible portions of the table. The update happens when new rows are added or removed during scrolling. Some code:
class ViewController: UIViewController, UITableViewDelegate {
@IBOutlet var slider: UISlider!
@IBOutlet var tableView: UITableView!
let maxCount = 50
var multiplierArray: [String] = []
override func viewDidLoad() {
super.viewDidLoad()
let number: Int = Int(slider.value)
populateData(number)
}
func populateData(number: Int) {
var i = 0
multiplierArray.removeAll()
while i < maxCount {
let table = i + 1
let str = "\(number) x \(table) = \(table * number)"
print(str)
multiplierArray.append(str)
i += 1
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return multiplierArray.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")
cell.textLabel?.text = multiplierArray[indexPath.row]
print("cellForRowAtIndexPath called")
return cell
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int{
return 1
}
@IBAction func sliderValueChanged(sender: UISlider) {
let number: Int = Int(slider.value)
populateData(number)
tableView.reloadData()
print("slider value changed" + String(number))
}
}
I read quite a few posts on this issue but couldn't figure out the issue in above code. My understanding is that tableView.reloadData() should update the view when the underlying model is changed like notifyDataSetChanged() in android. Am I missing some functions of tableView?