我正在进入这个程序堆栈或停止工作
问题描述:
这是我的程序我是初学者构建成功,但停止运行。我正在进入这个程序堆栈或停止工作
import UIKit
class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
var items = [String]()
@IBOutlet var textField: UITextField!
@IBOutlet weak var tableView: UITableView!
@IBAction func addButton(sender: UIButton) {
let newItem = textField.text
items.append(newItem!)
textField.resignFirstResponder()
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
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
您datasource
尝试增加它在你的IBAction为这样
@IBAction func addButton(sender: UIButton) {
let newItem = textField.text
items.append(newItem!)
tableView.reloadData()
textField.resignFirstResponder()
}
添加reloaData后缺少
tableView.reloadData()
没有帮助我 – Israel