2016-12-08 9 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

ストーリーボードの表ビューセルを選択し、字幕付きスタイルを設定してください

+0

問題はありませんaiはすべて正しく仮定しています。クラスすなわちするvar名前と番号での二つの変数はので、私はちょうどあなたが –

+0

が行う一度私のコードを通過...同じセルに解析カント{ は結果= はmanagedContext.executeFetchRequest(fetchRequest)を試みるなど 名=結果を聞かせて! [NSManagedObject] numbers = results as! [NSManagedObject] プリント(結果) }キャッチせNSError { プリント( "\ \(error.userInfo)を(エラー)を取得できませんでした") } – aahmetbas

+0

名前と番号であるとしてエラーが同じ結果 – aahmetbas