2015-08-10 1 views
2

В этом представлении таблицы (override func tableView) показаны показывающие точки интереса, некоторые загружаются по умолчанию, другие - пользователем. Я могу получить имя управляемого объекта, но при попытке показать позицию он терпит неудачу.Невозможно показать Double из CoreData в UILabel внутри TableViewCell

Я не могу показать широту и долготу в TableViewCell. Если я использую ключевое слово name, появится имя. Может быть, мне нужно сделать какое-то преобразование из чисел, но где?

Я также получил это предупреждение:

CoreData: предупреждение: Не удалось загрузить класс с именем 'PRODUCT_MODULE_NAME.ArcheoPOI' для лица 'ArcheoPOI. Класс не найден, вместо этого используется NSManagedObject по умолчанию.

Это мой NSManagedContext

enter image description here

import UIKit 
import CoreData 

import CoreLocation 

class AppiaTVC: UITableViewController, UITableViewDataSource, UITableViewDelegate { 


    var places : [NSManagedObject] = [] 

    let myLocationManager = CLLocationManager() 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     //MARK : good way to set the reuse id of the cell 
     //  tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "Cell") //alternative to put it in the 
     //MARK : good way to set the table title 
     title = "\"All the POI\"" 


    } 





    //MARK: - Added for fetching data from CoreData 
    override func viewWillAppear(animated: Bool) { 
     super.viewWillAppear(animated) 

     //1 
     let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate 

     let managedContext = appDelegate.managedObjectContext!  //needed to handel mangedObject (saving and fetching entries) 

     //2 
     let fetchRequest = NSFetchRequest(entityName:"ArcheoPOI") 

     //3 
     var error: NSError? 

     let fetchedResults = managedContext.executeFetchRequest(fetchRequest, error: &error) as? [NSManagedObject] 

     if let results = fetchedResults { 
      places = results 
     } else { 
      println("Could not fetch \(error), \(error!.userInfo)") 
     } 
    } 




    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

    // MARK: - Table view data source 

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
     // #warning Potentially incomplete method implementation. 
     // Return the number of sections. 
     return 1 
    } 

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     // #warning Incomplete method implementation. 
     // Return the number of rows in the section. 
     return places.count 
    } 


    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

     let cell = tableView.dequeueReusableCellWithIdentifier("appiaCellID", forIndexPath: indexPath) as! AppiaTVCell 

     cell.backgroundColor = UIColor.blueColor() 
     if indexPath.row % 2 == 0 { 
      //   cell.backgroundColor = UIColor(red: 143/255, green: 204/255, blue: 255/255, alpha: 1) 
      //   cell.sweetTextView.backgroundColor = UIColor(red: 63/255, green: 159/255, blue: 255/255, alpha: 1) 

     } else { 
      //   cell.backgroundColor = UIColor(red: 63/255, green: 159/255, blue: 255/255, alpha: 1) 
      //   cell.sweetTextView.backgroundColor = UIColor(red: 143/255, green: 204/255, blue: 255/255, alpha: 1) 
     } 


     let singlePlace = places[indexPath.row] 
     //KVC is the only way to access data in CoreData (can't use a property) butwe could also use a more natural object-style person.name 
     //  cell.textLabel!.text = singlePlace.valueForKey("name") as? String 
     cell.cellNameLabel.text = singlePlace.valueForKey("name") as? String 
     cell.cellLatitudeLabel.text = singlePlace.valueForKey("latitude") as? String //if I use "name" it works 
     cell.cellLongitudeLabel.text = singlePlace.valueForKey("longitude") as? String //if I use "name" it works 

     return cell 
    } 




    @IBAction func addPlaceButtonPressed(sender: UIBarButtonItem) { 

     var alert = UIAlertController(title: "New place", message: "Add a new place", preferredStyle: .Alert) 

     let saveAction = UIAlertAction(title: "Save", style: .Default) { (action: UIAlertAction!) -> Void in 

      let textField = alert.textFields![0] as! UITextField 
      self.saveName(textField.text) //self.names.append(textField.text) 
      self.tableView.reloadData() 
     } 

     let cancelAction = UIAlertAction(title: "Cancel", style: .Default) { (action: UIAlertAction!) -> Void in 
     } 

     alert.addTextFieldWithConfigurationHandler { 
      (textField: UITextField!) -> Void in 
     } 

     alert.addAction(saveAction) 
     alert.addAction(cancelAction) 

     presentViewController(alert, 
      animated: true, 
      completion: nil) 

    } 




    //MARK: - save data to CoreData - this function is called by the addName button 
    func saveName(name: String) { 
     //1 
     let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate 

     let managedContext = appDelegate.managedObjectContext! 

     //2 
     let entity = NSEntityDescription.entityForName("ArcheoPOI", inManagedObjectContext: managedContext) //NSEntityDescription is required 

     let place = NSManagedObject(entity: entity!, insertIntoManagedObjectContext:managedContext) 


     myLocationManager.startUpdatingLocation() 
     let lat = myLocationManager.location.coordinate.latitude 
     let lon = myLocationManager.location.coordinate.longitude 
     myLocationManager.stopUpdatingLocation() 
     println("************************") 
     println(lat) 
     println(lon) 
     println("************************") 


     //3 
     place.setValue(name, forKey: "name") 
     place.setValue(lat, forKey: "latitude") 
     place.setValue(lon, forKey: "longitude") 

     //4 
     var error: NSError? 
     if !managedContext.save(&error) { 

      println("Could not save \(error), \(error?.userInfo)") 

     } 
     //5 
     places.append(place) 
    } 




    //MARK - delete NSManagedObject 
    override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { 

     switch editingStyle { 

     case UITableViewCellEditingStyle.Delete: 
      // remove the deleted item from the model 
      let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate 
      let managedContext:NSManagedObjectContext = appDelegate.managedObjectContext! 


      managedContext.deleteObject(places[indexPath.row] as NSManagedObject) 
      places.removeAtIndex(indexPath.row) 

      managedContext.save(nil) 

      //tableView.reloadData() 
      // remove the deleted item from the `UITableView` 
      self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade) 

     default: 
      return 

     } 
    } 




    @IBAction func BackButtonPressed(sender: UIBarButtonItem) { 

     dismissViewControllerAnimated(true , completion: nil) 

    } 


} 

ответ

0

Изменить код

let latitude = singlePlace.valueForKey("latitude") 
let longitude = singlePlace.valueForKey("longitude") 
cell.cellLatitudeLabel.text = "\(latitude)" //if I use "name" it works 
cell.cellLongitudeLabel.text = "\(longitude)" //if I use "name" it works 

Я также настоятельно рекомендую вам создать класс, который будет управлять вашей сущности. Вы можете найти хорошие инструкции о том, как это сделать. here. Это руководство в Objective-C, но с тех пор ничего не изменилось;)

Вы тогда сможете позвонить singlePlace.latitutde вместо использования valueForKey,

+0

проблема с вашим кодом: Неожиданный символ '' 'в строковой интерполяции – biggreentree

+0

Извините, мой плохой. Я обновил свой код –

+0

благодарю вас за помощь и советы, но мне пришлось добавить «как! NSNumber ". Есть ли способ посмотреть и отредактировать то, что у меня есть в моей базе данных? Я хотел бы добавить много объектов из списка через Xcode – biggreentree

0

Это для CoreData предупреждения вы упомянули:

Когда просмотреть объект на файл .xcdatamodeld, откройте вкладку Utilities и посмотрите раздел Data Model inspector. Существует выпадающий список для Module. По состоянию на некоторые из новых бета-версий Xcode 7, он скажет Current Product Module по умолчанию. Удалите это и оставьте поле пустым. Это должно устранить вашу проблему.

+0

только выпадающее меню, я вижу, «материнская компания» – biggreentree

+0

Какие Версия Xcode вы используете? Это было предупреждение, которое я видел только с Xcode 7b3 или 7b4. Если вы используете более ранний Xcode, тогда это выпадающее меню может отсутствовать, и предупреждение может быть от чего-то другого. – keithbhunter

+0

Не бета, Xcode 6.4, возможно, я не должен использовать структуру и использовать 100% coreData, как это было предложено в ответе выше ... – biggreentree

Смежные вопросы