在 UITableViewController 中为多个实例初始化结构的位置

Where to initialize struct for multiple instances in UITableViewController

我正在开发一个 task-sharing 应用程序,您可以在其中与多人共享多个任务列表(例如与您的配偶共享一个,与您的孩子共享一个,等等)。每个列表的数据源都是一个名为 TaskList 的结构,因此我将有多个 TaskList 的实例。我应该在哪里初始化这些实例?

我是新手,如有任何帮助,我们将不胜感激!

这是 UITableViewController 使用 struct TaskList 创建任务列表的代码:

import UIKit

class LoLFirstTableViewController: UITableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.rowHeight = UITableViewAutomaticDimension
        tableView.estimatedRowHeight = 60.0
    }

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

    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

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

    @IBAction func cancelToLoLFirstTableViewController(_ segue:UIStoryboardSegue) {
    }

    @IBAction func saveAddTask(_ segue:UIStoryboardSegue) {
        if let AddTaskTableViewController = segue.source as? AddTaskTableViewController {

            if let task = AddTaskTableViewController.task {
                tasks.append(task)

                let indexPath = IndexPath(row: tasks.count-1, section: 0)
                tableView.insertRows(at: [indexPath], with: .automatic)
            }
        }
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "TaskCell", for: indexPath) as! TaskCell
        let task = tasks[indexPath.row]
            cell.task = task

        if cell.accessoryView == nil {
            let cb = CheckButton()
            cb.addTarget(self, action: #selector(buttonTapped(_:forEvent:)), for: .touchUpInside)
            cell.accessoryView = cb
        }
        let cb = cell.accessoryView as! CheckButton
        cb.check(tasks[indexPath.row].completed)

        return cell
    }

    func buttonTapped(_ target:UIButton, forEvent event: UIEvent) {
        guard let touch = event.allTouches?.first else { return }
        let point = touch.location(in: self.tableView)
        let indexPath = self.tableView.indexPathForRow(at: point)

        var tappedItem = tasks[indexPath!.row] as Task
        tappedItem.completed = !tappedItem.completed
        tasks[indexPath!.row] = tappedItem

        tableView.reloadRows(at: [indexPath!], with: UITableViewRowAnimation.none)
    }
}

这是任务列表的代码:

import UIKit

struct TaskList {
   var buddy: String
   var phoneNumber: String
   var tasks: [Task]
}

第一个选项。您可以创建某种管理器并将该列表存储在该对象中:

class TaskListsManager
{
  var taskLists:[TaskList] = []
}

然后在您的视图控制器之间传递该对象的实例:

let taskListsManager = TaskListsManager()

firstViewController.taskListsManager = taskListsManager
secondViewController.taskListsManager = taskListsManager

第二个选项是创建一个单例对象:

class TaskListsManager
{
  static let sharedInstance = TaskListsManager()

  var taskLists:[TaskList] = []
}

而且您可以在应用中的任何地方使用它:

let first = TaskListsManager.sharedInstance.taskLists.first