Я новичок в стриж, учась http://www.raywenderlich.com «s учебник ..Swift ViewController numberOfRowsInSection вопрос
Я написал этот контроллер:
import UIKit
class ViewController: UIViewController, UITableViewDataSource {
@IBOutlet var totalTextField : UITextField!
@IBOutlet var taxPctSlider : UISlider!
@IBOutlet var taxPctLabel : UILabel!
@IBOutlet var resultsTextView : UITextView!
let tipCalc = TipCalculatorModel(total: 33.25, taxPct: 0.06)
var possibleTips = Dictionary<Int, (tipAmt:Double, total:Double)>()
var sortedKeys:[Int] = []
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
self.refreshUI()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func refreshUI() {
totalTextField.text = String(format: "%0.2f", tipCalc.total)
taxPctSlider.value = Float(tipCalc.taxPct) * 100.0
taxPctLabel.text = "Tax Percentage (\(Int(taxPctSlider.value))%"
}
@IBAction func calculateTapped(sender : AnyObject) {
tipCalc.total = Double((totalTextField.text as NSString).doubleValue)
possibleTips = tipCalc.returnPossibleTips()
sortedKeys = sorted(Array(possibleTips.keys))
tableView.reloadData()
}
@IBAction func taxPercentageChanged(sender: AnyObject) {
tipCalc.taxPct = Double(taxPctSlider.value)/100
refreshUI()
}
@IBAction func viewTapped(sender: AnyObject) {
totalTextField.resignFirstResponder()
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return sortedKeys.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.Value2, reuseIdentifier: nil)
let tipPct = sortedKeys[indexPath.row]
let tipAmt = possibleTips[tipPct]!.tipAmt
let total = possibleTips[tipPct]!.total
cell.textLabel?.text = "\(tipPct)%:"
cell.detailTextLabel?.text = String(format: "Tip: $%0.2f, Total: $%0.2f", tipAmt, total)
return cell
}
}
, когда я пытаюсь запустить это приложение, я получаю следующее сообщение об ошибке :
-[UIView tableView:numberOfRowsInSection:]: unrecognized selector sent to instance
, к сожалению, я новичок в этом языке программирования и программирования для Айфона, так что я понятия не имею, как решить эту проблему.
Я был бы признателен за вашу помощь в этой манере
как я могу это исправить? Я попытался написать init-функцию, но это дает мне больше ошибок. –
Вы могли бы сначала «numberOfRowsInSection» вернуть статическое число ячеек, или если вы хотите, чтобы он возвращал массив 'sortedKeys', вы должны заполнить массив со значениями и не должны быть пустыми при загрузке. – Aaron