0

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?

1
  • Can you show us if you assigned your table view's delegate and data source to your current view controller? Commented Jul 20, 2016 at 9:52

1 Answer 1

1

Your ViewController class needs to implement the UITableViewDataSource protocol, no need to implement the UITableViewDelegate here. And you need to assign it in the viewDidLoad function (for example):

override func viewDidLoad() {
    super.viewDidLoad()
    let number: Int = Int(slider.value)
    populateData(number)
    tableView.datasource = self;
}

Quick note: UITableViewDatasource is the one in charge for populating the UITableView with data. UITableViewDelegate cares about cells height and selecting states.

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

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.