【问题标题】:how to put 2 text in 1 row when you add?添加时如何将 2 个文本放在 1 行中?
【发布时间】:2016-12-12 00:43:12
【问题描述】:

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)")
        }
    }
}

【问题讨论】:

  • 您能否改进问题的格式?我不明白这里问的是什么。 (不要在 cmets 中回答 - 改进您的问题:D)
  • 你能看到图片吗?
  • 我需要表格视图中 1 行中的文本 (a) 和文本 (b) 看图片,请您看不懂

标签: ios core-data swift3 xcode8


【解决方案1】:

为什么不在表格视图单元格中使用两个标签/文本字段? 为此,您将需要一个自定义的 uitableviewcell。

【讨论】:

  • 我需要每个单独的文本与其他 ، example firest text = job secen text = age 等等
  • 这就是为什么您应该使用具有多个标签的自定义 tableviewcell 的原因。如果您想轻松跟踪您的输入
  • 因为我必须排列
  • 有没有更好的办法来解决这些问题?
猜你喜欢
  • 2022-01-15
  • 2020-06-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-30
  • 2020-02-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多