我用Table View
和Segmented Control
做了一个应用,这是我第一次.我使用了一些代码和教程,但不起作用.当我运行我的应用程序时,它正在崩溃,并在日志(log)中显示此错误:
MyApplication[4928:336085]*由于未捕获的异常"NSUnknownKeyException"而终止应用程序,原因:"[setValue:forUndefinedKey:]:此类不符合key tableView的键值编码."
我使用的代码是:
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
let foodList:[String] = ["Bread", "Meat", "Pizza", "Other"]
let drinkList:[String] = ["Water", "Soda", "Juice", "Other"]
@IBOutlet weak var mySegmentedControl: UISegmentedControl!
@IBOutlet weak var myTableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
var returnValue = 0
switch(mySegmentedControl.selectedSegmentIndex) {
case 0:
returnValue = foodList.count
break
case 1:
returnValue = drinkList.count
break
default:
break
}
return returnValue
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let myCell = tableView.dequeueReusableCellWithIdentifier("myCells", forIndexPath: indexPath)
switch(mySegmentedControl.selectedSegmentIndex) {
case 0:
myCell.textLabel!.text = foodList[indexPath.row]
break
case 1:
myCell.textLabel!.text = drinkList[indexPath.row]
break
default:
break
}
return myCell
}
@IBAction func segmentedControlActionChanged(sender: AnyObject) {
myTableView.reloadData()
}
这是main.Storyboard美元
我判断了很多次代码,但都不起作用.首先,我只需要使用Table View
,观看本教程(https://www.youtube.com/watch?v=ABVLSF3Vqdg),我认为使用Segmented Control
也可以,就像在教程中一样.但还是不行.同样的代码,同样的错误.