を使用して、テーブルのセルに結果をフェッチするために、私は同じセル内に連絡先の名前と番号を取得したいが、それは私がここに私の全体のコードを追加した第二のセルに最初のセルと番号で名前をフェッチします....どのように迅速
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.
}
カスタムTableViewCellを使用していますか? – OverD
はい....カスタムセルを使用しています –
UITableViewCellのサブクラスには何を挙げましたか?そして、あなたは@IBOutletsすべて接続されています – OverD