简体   繁体   English

如何通过按“添加”按钮添加新单元格并将文本保存到Coredata中

[英]How to add a new cell by press a Add button and save the text into Coredata

I have a ViewController with tableview and a separate nib for create the table view cell,the cell contains a textview. 我有一个带有tableview的ViewController和一个用于创建表视图单元的单独nib,该单元格包含一个textview。 press the add button,then the tableview will create a new cell,so i can input text into the textView. 按下添加按钮,然后tableview将创建一个新单元格,因此我可以将文本输入到textView中。 i want to insert cells to first row each time ,no matter how many cells existed. 我希望每次都将细胞插入第一行,无论存在多少个细胞。 Finally ,press the done button to save the text to coredata. 最后,按完成按钮将文本保存到coredata。

the xib Entities name is "Checklist",the xib file name is "ChecklistCell" xib实体名称为“Checklist”,xib文件名为“ChecklistCell”

class ChecklistViewController: 

    class ChecklistViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {

    var items = [Checklist]()

    var cellTextView:ChecklistCell!

    @IBOutlet weak var tableView: UITableView!

    @IBOutlet var toolBar: UIToolbar!

    let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext

    struct TableViewCellIdentifiers{
        static let checklistCell = "ChecklistCell"
    }

    @IBAction func addItem(){

       //i don't know how to write this code

    }

    @IBAction func toolBarDoneButton(_ sender: UIBarButtonItem) {
        //write data to coredata
        let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
        let item = Checklist(context: context)

        item.textView = cellTextView.textView.text

        (UIApplication.shared.delegate as! AppDelegate).saveContext()


    }
    override func viewDidLoad() {
        super.viewDidLoad()

        let cellNib = UINib(nibName: "ChecklistCell", bundle: nil)
        tableView.register(cellNib, forCellReuseIdentifier: "ChecklistCell")
    }

    override func viewWillAppear(_ animated: Bool) {
        getData()
        tableView.reloadData()
    }

    func getData(){
        do {
            items = try context.fetch(Checklist.fetchRequest())
        } catch {
            print("Fetching Failed")
        }
    }

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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: TableViewCellIdentifiers.checklistCell, for: indexPath) as! ChecklistCell

        let item = items[indexPath.row]

        if let myItem = item.textView {
            cell.textView.text = myItem
        }

        cell.textView.inputAccessoryView = toolBar

        return cell

    }



}

items = [Checklist()] + items will add a new Checklist object at the beginning (index [0] ) of your items array. items = [Checklist()] + items将在items数组的开头(index [0] )添加一个新的Checklist对象。 If you were using a custom init method for your Checklist class, you would want to also fill in those init params here, like Checklist(/*init params here*/) . 如果你在Checklist类中使用自定义init方法,你也想在这里填写那些init参数,比如Checklist(/*init params here*/)

To update your tableView for the user once you've altered its data source there is this answer: How to insert new cell into UITableView in Swift , which I have included in the below code. 为了更新数据源,为用户更新tableView,有以下答案: 如何在Swift中将新单元格插入UITableView ,我已将其包含在下面的代码中。

For Swift 3.0 your addItem() function might look like: 对于Swift 3.0, addItem()函数可能如下所示:

func addItem() {

    items = [Checklist()] + items //adds Checklist item at beginning of array

    tableView.beginUpdates()
    tableView.insertRows(at: [IndexPath(row: 0, section: 0)], with: .automatic) //row param is 0 for first row
    tableView.endUpdates()
}

Hope that helps. 希望有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM