簡體   English   中英

如何通過按“添加”按鈕添加新單元格並將文本保存到Coredata中

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

我有一個帶有tableview的ViewController和一個用於創建表視圖單元的單獨nib,該單元格包含一個textview。 按下添加按鈕,然后tableview將創建一個新單元格,因此我可以將文本輸入到textView中。 我希望每次都將細胞插入第一行,無論存在多少個細胞。 最后,按完成按鈕將文本保存到coredata。

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將在items數組的開頭(index [0] )添加一個新的Checklist對象。 如果你在Checklist類中使用自定義init方法,你也想在這里填寫那些init參數,比如Checklist(/*init params here*/)

為了更新數據源,為用戶更新tableView,有以下答案: 如何在Swift中將新單元格插入UITableView ,我已將其包含在下面的代碼中。

對於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()
}

希望有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM