【问题标题】:Where to initialize struct for multiple instances in UITableViewController在 UITableViewController 中为多个实例初始化结构的位置
【发布时间】:2017-12-25 04:05:43
【问题描述】:

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

我是这方面的新手,任何帮助将不胜感激!

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

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]
}

【问题讨论】:

    标签: ios arrays swift uitableview instance-variables


    【解决方案1】:

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

    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
    

    【讨论】:

    • @BobF。有点。如果您只想拥有一个管理器实例,则必须在视图控制器之间传递它。因此,您在应用程序启动时创建一个 TaskListsManager 实例,然后将其传递给您的第一个视图控制器,然后如果您需要导航到另一个视图控制器,您可以将您的管理器实例从第一个控制器传递到第二个控制器。数据更新是一个不同的问题,为此您可以使用通知 (developer.apple.com/documentation/foundation/notificationcenter) 或协议/委托。
    • @BobF。并不真地。看看完整的例子:pastebin.com/PZ6i7SHf
    • @BobF。很高兴它有帮助。如果您认为它有帮助,请接受答案。
    • 是的,您需要为您的第二个视图控制器传递一个故事板 ID。
    • storyboard 变量是 UIViewController 的一个属性,所以你不需要创建它。你的 LoLFirstViewController 应该已经有了它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-20
    • 2017-12-21
    • 1970-01-01
    相关资源
    最近更新 更多