Search code examples
swiftswift2uisliderxcode9.2

Swift 2 errors with slider


I got those errors using swift in Xcode Version 9.2 (9C40b)

  1. Binary operator '*' cannot be applied to operands of type 'IndexPath' and 'Float'
  2. Ambiguous reference to member 'tableView(_:numberOfRowsInSection:)'
@IBOutlet weak var slider: UISlider!

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

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "Cell")
    cell.textLabel?.text = String(indexPath * slider.value)
    return cell
}

@IBAction func sliderValueChange(_ sender: Any) {
    tableView.reloadData()
}

Solution

  • Try this :

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
        let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "Cell")
        cell.textLabel?.text = String(slider.value * Float(indexPath.row))
        return cell
    
    }