2016-12-08 50 views
0

我想在同一个小区获取联系人姓名和号码,但在第二单元第一单元和数字取的名字我已经添加了我的整个编码这里....如何使用获取表格单元格的结果迅速

import UIKit 
import AddressBook 
import CoreData 
class ContactViewController: UIViewController,CNContactPickerDelegate,UITableViewDelegate,UITableViewDataSource{ 
     var contactStore = CNContactStore() 
    var mycontact:CNContactStore! 
    var myContacts = [CNContact]() 
    var names = [NSManagedObject]() 
    var numbers = [NSManagedObject]() 
    var yourcont:NSMutableArray = NSMutableArray() 
    @IBOutlet weak var FirstTableView: UITableView! 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     navigationController!.navigationBar.barTintColor = UIColor.blueColor() 
     navigationController!.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()] 
     // Do any additional setup after loading the view. 


    } 

    @IBAction func AddButtonTapped(sender: AnyObject) { 

     let alert = UIAlertController(title: "New Contact", 
             message: "Add a new contact", 
             preferredStyle: .Alert) 

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

             let textField = alert.textFields![0] as UITextField 
             self.saveName(textField.text!) 

             let sedfield = alert.textFields![1] as UITextField 
             self.saveNum(sedfield.text!) 
             self.FirstTableView.reloadData() 
     }) 

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

     alert.addTextFieldWithConfigurationHandler { 
      (textField: UITextField) -> Void in 
      textField.placeholder = "Enter Your Name" 

     } 
     alert.addTextFieldWithConfigurationHandler { 
      (sedfield: UITextField) -> Void in 
      sedfield.placeholder = "Enter your Number" 

     } 

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

     presentViewController(alert, 
           animated: true, 
           completion: nil) 
    } 
    func saveName(name: String) { 

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

     let managedContext = appDelegate.managedObjectContext 


     let entity = NSEntityDescription.entityForName("Person", 
                 inManagedObjectContext:managedContext) 

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


     person.setValue(name, forKey: "name") 


     do { 
      try managedContext.save() 

      names.append(person) 
     } catch let error as NSError { 
      print("Could not save \(error), \(error.userInfo)") 
     } 
    } 
    func saveNum(name: String) { 

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

     let managedContext = appDelegate.managedObjectContext 


     let entity = NSEntityDescription.entityForName("Person", 
                 inManagedObjectContext:managedContext) 

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


     person.setValue(name, forKey: "number") 


     do { 
      try managedContext.save() 

      numbers.append(person) 
     } catch let error as NSError { 
      print("Could not save \(error), \(error.userInfo)") 
     } 
    } 
    @IBAction func LeftTapped(sender: AnyObject) { 
     let appDelegate:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate 
     appDelegate.centerContainer?.toggleDrawerSide(MMDrawerSide.Left, animated: true, completion: nil) 
      } 

      func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
      if(section == 0) { 
       return names.count 
      } 
       return numbers.count 
    } 
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("ContactCell") as! TableViewCell 
     let personname = names[indexPath.row] 
    cell.ConName.text = personname.valueForKey("name") as? String 
    let personnum = numbers[indexPath.row] 
    cell.ConNumber.text = personnum.valueForKey("number") as? String 

     return cell 
    } 
    func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool 
    { 
     return true 
    } 

    func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) 
    { 
     if editingStyle == .Delete 
     { 
      numbers.removeAtIndex(indexPath.row) 
      tableView.reloadData() 
     } 
    } 
    override func viewWillAppear(animated: Bool) { 
     super.viewWillAppear(animated) 


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

     let managedContext = appDelegate.managedObjectContext 


     let fetchRequest = NSFetchRequest(entityName: "Person") 


     do { 
      let results = 
       try managedContext.executeFetchRequest(fetchRequest) 
      names = results as! [NSManagedObject] 
      numbers = results as! [NSManagedObject] 

      print(results) 
     } catch let error as NSError { 
      print("Could not fetch \(error), \(error.userInfo)") 
     } 
    } 
      override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 
+0

您是否使用自定义TableViewCell? – OverD

+0

是....正在使用自定义单元格 –

+0

你是什么名称的UITableViewCell的子类?你是@IBOutlets全部连接 – OverD

回答

0

在Storyboard上选择表格视图单元格并设置字幕样式

+0

这不是一个问题ai已经正确地采用了一切,因为我已经假设我的编码存在问题在类中的两个变量,即var名称和数字,所以我不能解析在同一个单元格中...只要你通过我的代码一次 –

+0

做{ 让结果= 尝试managedContext.executeFetchRequest(fetchRequest) names = results as! [NSManagedObject] numbers = results as! [NSManagedObject] 打印(结果) }赶上让误差NSError { 打印( “无法获取\(错误),\(error.userInfo)”) } – aahmetbas

+0

名称和号码是否等于同样的结果 – aahmetbas

相关问题