如何在列表视图中显示新项目

时间:2018-06-14 21:35:15

标签: ios swift uitableview

我正在Xcode 9.4中构建一个应用程序,我在另一个视图控制器中创建新项目后无法在列表视图中显示它。该项目显示在CoreData中,但为了使项目在列表视图中显示,我必须退出列表视图,然后返回列表中查看它。

我添加了Protocol和Delegate方法,但公式中仍然缺少某些内容。

以下是添加新项目的DetailViewController中的代码:

import UIKit
import CoreData


protocol DetailViewControllerDelegate: class {
  func detailViewController(_ controller: DetailViewController, didFinishAdding task: Task)
 // func detailViewController(_ controller: DetailViewController, didFinishEditing task: Task)
}

class DetailViewController: UIViewController, UITextFieldDelegate, UITextViewDelegate {

  var tasks = [Task]()

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

  var selectedTask: String?
  var taskDetails: String?
  var taskCategory: Category?

  @IBOutlet weak var taskTitle: UITextField!
  @IBOutlet weak var taskDetail: UITextView!

  var taskToEdit: Task?

  weak var delegate: DetailViewControllerDelegate? // Required to carry out the protocol methods

    override func viewDidLoad() {
        super.viewDidLoad()

      taskTitle.text = selectedTask
      taskDetail.text = taskDetails

      taskDetail.textColor = UIColor.lightGray
      navigationItem.largeTitleDisplayMode = .never
    }

  func textViewDidBeginEditing(_ textView: UITextView) {
    if taskDetail.textColor == UIColor.lightGray {
      taskDetail.text = nil
      taskDetail.textColor = UIColor.black
    }
  }

  func textViewDidEndEditing(_ textView: UITextView) {
    if taskDetail.text.isEmpty {
      taskDetail.text = "Details..."
      taskDetail.textColor = UIColor.lightGray
    }
  }

  override func viewWillAppear(_ animated: Bool) {
    taskTitle.becomeFirstResponder()
  }


  @IBAction func doneButtonPressed(_ sender: UIBarButtonItem) {
    let newTask = Task(context: self.context)
    newTask.title = taskTitle.text!
    newTask.details = taskDetail.text!
    newTask.parentCategory = taskCategory!

    delegate?.detailViewController(self, didFinishAdding: newTask)

    // self.taskArray.append(newTask)
    self.saveTasks()

    navigationController?.popViewController(animated: true)

  }


  func saveTasks() {

    do {
      try context.save()
    } catch {
      print("Error saving context \(error)")
    }
  }

}

以下是列表视图控制器中的代码,它应该在创建后立即显示新项目:

import UIKit
import CoreData

class TaskListTableViewController: UITableViewController, DetailViewControllerDelegate  {

    var tasks = [Task]()
    var selectedCategory: Category? {
        didSet {
            loadTasks()
        }
    }
    let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext



    override func viewDidLoad() {
        super.viewDidLoad()

        navigationItem.largeTitleDisplayMode = .never

        title = selectedCategory?.name
        loadTasks()
        tableView.reloadData()

        // self.navigationItem.rightBarButtonItem = self.editButtonItem
    }

  func detailViewController(_ controller: DetailViewController, didFinishAdding task: Task) {

    let newRowIndex = tasks.count
    tasks.append(task)
    let indexPath = IndexPath(row: newRowIndex, section: 0)
    let indexPaths = [indexPath]
    tableView.insertRows(at: indexPaths, with: .automatic)
    tableView.reloadData()

    navigationController?.popViewController(animated: true)
  }


  // TODO: finish this
  func detailViewController(_ controller: DetailViewController, didFinishEditing task: Task) {

  }


    // MARK: - Table view data source

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


    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "TaskCell", for: indexPath)

        let task = tasks[indexPath.row]
        let label = cell.viewWithTag(1001) as! UILabel
        label.text = task.title

        return cell
    }


    // MARK: - Navigation

  override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "AddItem" {
      let destinationVC = segue.destination as! DetailViewController
      destinationVC.taskCategory = selectedCategory
    } else if segue.identifier == "EditItem" {
      let destinationVC = segue.destination as! DetailViewController
      destinationVC.taskCategory = selectedCategory
      let indexPath = tableView.indexPathForSelectedRow!
      let task = tasks[indexPath.row]
      destinationVC.selectedTask = task.title
      destinationVC.taskDetails = task.details

    }
  }


  func loadTasks(with request: NSFetchRequest<Task> = Task.fetchRequest(), predicate: NSPredicate? = nil) {

    let categoryPredicate = NSPredicate(format: "parentCategory.name MATCHES %@", selectedCategory!.name!)

    request.predicate = categoryPredicate

    do {
      tasks = try context.fetch(request)
    } catch {
      print("Error fetching data \(error)")
    }

    tableView.reloadData()
  }

  @IBAction func addTaskButton(_ sender: UIBarButtonItem) {
    print("new task added")
  }

}

1 个答案:

答案 0 :(得分:1)

您需要在prepareForSegue

中设置委托
destinationVC.delegate = self