我目前正在尝试使用这些代码加载我的tableview,但是在返回 listitem.count 时我得到一个线程断点。任何帮助解决这个问题都会很棒。
以下是我的代码:
import UIKit
import CoreData
class ViewInventoryTableController: UITableViewController{
var listItems = [NSManagedObject]()
override func viewDidLoad(){
super.viewDidLoad()
//Do any additional setup after loading the view, typically from a nib.
self.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.add, target: self, action: #selector(ViewInventoryTableController.addItem))
}
func addItem(){
let alertMessage = "Please key in the activation code"
let alertController = UIAlertController(title: "Add Item", message: alertMessage as String, preferredStyle: .alert)
//Add the textfield
var activationCodeTextField1: UITextField?
alertController.addTextField { (textField) -> Void in
activationCodeTextField1 = textField
activationCodeTextField1?.placeholder = "123456789"
}
// Create Cancel button
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (action:UIAlertAction!) in
print("Cancel button tapped")
}
alertController.addAction(cancelAction)
// Create ConfirmAction
let confirmAction = UIAlertAction(title: "Confirm", style: UIAlertActionStyle.default, handler: ({
(_) in
if let field = alertController.textFields![0] as? UITextField{
self.saveItem(itemToSave: field.text!)
self.tableView.reloadData()
}
}
))
alertController.addAction(confirmAction)
// Present Dialog message
OperationQueue.main.addOperation {
self.present(alertController, animated: true){}
}
print("Activation Code = \(activationCodeTextField1?.text)")
}
//function save item
func saveItem(itemToSave: String){
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let managedContext = appDelegate.persistentContainer.viewContext
let entity = NSEntityDescription.entity(forEntityName: "ListEntity", in: managedContext)
let item = NSManagedObject(entity: entity!, insertInto: managedContext)
item.setValue(itemToSave, forKey: "item")
do {
try managedContext.save()
listItems.append(item)
}
catch {
print("error")
}
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let managedContext = appDelegate.persistentContainer.viewContext
tableView.reloadRows(at: [indexPath], with: UITableViewRowAnimation.right)
managedContext.delete(listItems[indexPath.row])
listItems.remove(at: indexPath.row)
self.tableView.reloadData()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
//Dispose of any resources that can be recreated
}
override func tableView (_ tableView: UITableView , numberOfRowsInSection section: Int) -> Int {
return listItems.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell")! as UITableViewCell
let item = listItems[indexPath.row]
cell.textLabel?.text = item.value(forKey: "item") as? String
return cell
}
}