TableViewControllerを使用しようとしていますが、私のコードクラッシュを実際には理解できないため、TableviewControllerを使用するとクラッシュします
異なるXCodeプロジェクトに同じコード(コピー貼り付け)が正常に実行されます。同時に
import UIKit
class ViewController: UITableViewController {
var listeDeListes = [AnyObject]()
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Add, target: self, action: #selector(ViewController.addItem))
if (NSUserdefault.standartDefault.objectForKey("identity") != nil) {
listeDeListes = NSUserDefaults.standardUserDefaults().arrayForKey("identity")!
}
}
func addItem() {
let alertController = UIAlertController(title: "New Resolution", message: "", preferredStyle: UIAlertControllerStyle.Alert)
let confirmAction = UIAlertAction(title: "Confirm", style: UIAlertActionStyle.Default, handler: ({
(_) in
if let field = alertController.textFields![0] as? UITextField {
self.saveItem(field.text!)
self.tableView.reloadData()
}
}))
let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil)
alertController.addTextFieldWithConfigurationHandler ({
(textField) in
textField.placeholder = "Type smothing..."
})
alertController.addAction(confirmAction)
alertController.addAction(cancelAction)
self.presentViewController(alertController, animated: true, completion: nil)
}
func saveItem(itemToSave: String) {
let defaults = NSUserDefaults.standardUserDefaults()
//defaults.setInteger(identity, forKey: "identity")
listeDeListes = listeDeListes + [itemToSave]
defaults.setObject(listeDeListes, forKey: "identity")
defaults.synchronize()
}
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 50
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return listeDeListes.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell")! as UITableViewCell
let item = listeDeListes[indexPath.row]
cell.textLabel?.text = item as! String
return cell
}
このコードは動作しdoes't作業:
は、ここに私のコードです。なぜ、どのように見つけるのを助けてください。 「悪い」プロジェクト」私はこのエラーメッセージを取得するには
にあなたのセルコードを変更してみてくださいもらえますか?ストーリーボード経由またはプログラムによるどちらの場合でも、セルは同じ再利用識別子で登録する必要があります。 – Echizzle
ストーリーボードには何もありません。私はストーリーボードのtableViewにクラスを設定しました。 – Matt
'うまく動作してクラッシュする。 '少し矛盾しているとは思わない。 – Dravidian