如何在Swift中的多个视图控制器中重用表视图? [英] How can I reuse a table view in multiple view controllers in Swift?

查看:103
本文介绍了如何在Swift中的多个视图控制器中重用表视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用程序,该应用程序需要在我的应用程序的多个场景中使用完全相同的表格视图,但是,表格的数据和位置将发生变化,如这两张图片所示(表格视图以高亮显示)红色)

I'm working on an app right now that requires the exact same table view in multiple scenes on my app, however, the data and the placement of the table will change, as illustrated in these two pictures (table view highlighted in red)

在整个应用中表格的所有实例中,该表格应具有:

In all instances of the table throughout the app, it should have:

  1. 相同的委托方法
  2. 相同的数据源方法(尽管实际数据将更改)
  3. 相同的表格视图单元格类型具有相同的出口和动作.
  4. 相同的内部约束(我不想每次都为表单元格手动添加约束).

现在,我将它们编码为两个单独的视图控制器中的两个单独的表视图,但是,我意识到我将需要在整个应用中的更多地方复制相同的表和逻辑,这并不让人感到喜欢正确的方法.所以我的问题是,如何才能以一种干净,干燥的方式在满足上述规格的iOS中完成表复制(使用界面构建器或swift)?

Right now I'm coding them as two separate table views in two separate view controllers, however, I've realized I will need to replicate the same table and logic in many more places throughout my app and this doesn't feel like the right way to go about. So my question is, how can you accomplish table replication in iOS (using interface builder or swift) that meets the specs above, in a clean and dry way?

推荐答案

如果要一次设计单元格,然后在不同视图控制器中的不同表视图中使用它们,则必须在单独的.xib中设计它们.文件.原型单元是每个视图控制器,并且扩展性不是很好.

If you want to design your cells once and then use them across different table views in different view controllers you have to design them in the separate .xib files. Prototype cells are per view controller and don't scale very well.

如果所有数据源和委托方法都相同,则可以将此协议的实现转移到单独的类.您可以使用要显示的项目数组来配置此类.

If all the data source and delegate methods are the same, you can move implementation of this protocols to the separate class. You can configure this class with the array of items to display.

class ReusableTableView: NSObject, UITableViewDataSource, UITableViewDelegate
{
    var tableView: UITableView
    var tableViewData: [String]

    init(_ tv: UITableView, _ data: [String])
    {
        tableViewData = data
        tableView = tv
        super.init()
        tableView.delegate = self
        tableView.dataSource = self

        // Register all of your cells
        tableView.register(UINib(nibName: "SomeNib", bundle: nil), forCellReuseIdentifier: "example-id")
    }

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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        return tableView.dequeueReusableCell(withIdentifier: "example-id", for: indexPath)
    }
}

有了这两个构建基块,您可以在每个视图控制器上分别布置表格视图,并将其与可重复使用的数据源/代理连接起来.

Having this two building blocks you can layout the table view separately on each view controller and wire it up with your reusable data source/delegate.

class ExampleTablewViewController: UIViewController
{
    @IBOutlet weak var tableView: UITableView!
    var reusableTableView: ReusableTableView!

    override func viewDidLoad() {
        super.viewDidLoad()

        reusableTableView = ReusableTableView(tableView, ["lorem", "ipsum", "dolor"])
        reusableTableView.tableView.reloadData()
    }
}

您可以在 GitHub上找到基本的示例回购.

这篇关于如何在Swift中的多个视图控制器中重用表视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆