添加时如何在1行中放入2个文本?

时间:2016-12-12 00:43:13

标签: ios core-data swift3 xcode8

enter image description here

import UIKit
import CoreData
class ViewController: UIViewController , UITableViewDataSource , UITableViewDelegate {
    @IBOutlet weak var text1: UITextField!
    @IBOutlet weak var text2: UITextField!
    @IBOutlet weak var ttableview: UITableView!
   /////
    //CoreData
    func saveName(name: String) {
        let  appDel:AppDelegate = UIApplication.shared.delegate as! AppDelegate
        let context:NSManagedObjectContext = appDel.persistentContainer.viewContext
        let entity =  NSEntityDescription.entity(forEntityName: "Zain",in:context)
        let person1 = NSManagedObject(entity: entity!,insertInto: context)
        let person2 = NSManagedObject(entity: entity!,insertInto: context)
         person1.setValue(name, forKey: "man1")
         person2.setValue(name, forKey: "man2")
        do {
            try context.save()
            //5
            array1.insert(person1, at: 0)
            array2.insert(person2, at: 0)
        } catch let error as NSError  {
            print("Could not save \(error), \(error.userInfo)")
        }
    }

    /////
    @IBAction func Add(_ sender: Any) {
        self.saveName(name: text1.text!)
        self.saveName(name: text2.text!)
        self.ttableview.reloadData()
    }
    //deleat row and tableview and arry
    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == .delete {
            array1.remove(at: indexPath.row)
            array2.remove(at: indexPath.row)
            ttableview.deleteRows(at: [indexPath], with: .fade)
            let appDel:AppDelegate = UIApplication.shared.delegate as! AppDelegate
            let context:NSManagedObjectContext = appDel.persistentContainer.viewContext
            context.delete(array1[indexPath.row] as NSManagedObject)
            context.delete(array2[indexPath.row] as NSManagedObject)
            do {
                try context.save()
            } catch _ {
                print("remove object error")
            }
        }
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
         return array1.count
    }
    func ttableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return array2.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = ttableview.dequeueReusableCell(withIdentifier: "cell") as! Cell
        let person1 = array1[indexPath.row]
        let person2 = array2[indexPath.row]
            cell.lable1.text = person1.value(forKey: "man1") as! String?
            cell.lable2.text = person2.value(forKey: "man2") as! String?
           return cell
    }

     var array1 = [NSManagedObject]()
     var array2 = [NSManagedObject]()

    override func viewDidLoad() {
        super.viewDidLoad()

        ttableview.delegate = self
        ttableview.dataSource = self

    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        let appDel:AppDelegate = UIApplication.shared.delegate as! AppDelegate
        let context:NSManagedObjectContext = appDel.persistentContainer.viewContext

        let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "Zain")

        do {
            let results = try context.fetch(fetchRequest)
            array1 = results as! [NSManagedObject]
            array2 = results as! [NSManagedObject]
        } catch let error as NSError {
            print("Could not fetch \(error), \(error.userInfo)")
        }
    }
}

1 个答案:

答案 0 :(得分:0)

为什么不在tableview单元格中使用两个标签/ textField? 你需要一个自定义的uitableviewcell。