Swift I';我进入这个程序栈,否则它就会停止工作
这是我的程序我是初学者构建成功但运行停止Swift I';我进入这个程序栈,否则它就会停止工作,swift,ios6,Swift,Ios6,这是我的程序我是初学者构建成功但运行停止 import UIKit class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { @IBOutlet weak var tableView: UITableView! @IBOutlet var textField: UITextField! var items = [String]() overrid
import UIKit
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var tableView: UITableView!
@IBOutlet var textField: UITextField!
var items = [String]()
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func addButton(sender: UIButton) {
let newItem = textField.text
items.append(newItem!)
textField.resignFirstResponder()
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell",forIndexPath: indexPath)
cell.textLabel?.text = items[indexPath.row]
cell.textLabel?.textColor = UIColor.redColor()
return cell
}
}
构建成功,运行时堆栈溢出中出现错误,显示为0或零
您能帮忙吗?我想您在向
数据源添加newItem
后缺少了一个tableView.reloadData()
试着像这样在IBAction中添加它
@IBAction func addButton(sender: UIButton) {
let newItem = textField.text
items.append(newItem!)
tableView.reloadData()
textField.resignFirstResponder()
}
添加重新加载的数据对我没有帮助