ios - 具有多个 TableView 的 Swift MVC 设计

标签 ios design-patterns swift

所以我的 Controller 的主视图有一个 TableView 。该 TableView 将显示许多不同的自定义类,这些类是 UITableViewCell 的子类。其中一些单元格内部也会有表格 View 。

我的问题是,在这种情况下,我不知道应该为 TableView 单元格中的 TableView 分配哪个类作为 UITableViewDelegate。我最初的想法是使其成为单元格 View 类:

class MyTableViewCell: TableViewCell {

    @IBOutlet var tableView: UITableView!;

    var messages: Array<String>?;

    //called by parent tableview when cellForRowAtIndexPath is called in main controller
    //to initialize view with dynamic properties at run time
    override func render(obj: MyObject) {
        messages = obj.getMessages();
    }
}

extension MyTableViewCell: UITableViewDataSource {

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {


    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return messages == nil ? 0 : messages!.count;
    }
}

这是有问题的,因为我没有地方将我的 nib 文件注册到 TableView :

nib = UINib(nibName: "MyTableViewCell", bundle : nil); self.tableView.registerNib(nib!, forCellReuseIdentifier: "custom");

此外,我觉得将 View 设为 TableView 数据源违反了 MVC 原则。在 TableView 单元格中处理 TableView 的最佳方法是什么?

最佳答案

UITableCell 中的 UITableView 确实是在 Pulse 中实现的,如 genalipsis 所描述的那样。在 Obj-C 中,有一个完整的教程以及发布的代码,位于此处,用于 UITableCell 中的 UITableView:

http://iosstuff.wordpress.com/2011/06/29/adding-a-uitableview-inside-a-uitableviewcell/

http://iosstuff.wordpress.com/2011/06/29/creating-pulse-style-scrolling-horizontally-scrolling-uitableview-as-a-subview-of-uitableviewcell/

这是在 Xcode 4 中完成的。我不确定这是否适用于 Xcode 6.1,但它确实描述了一种方法。

我认为 Ray Wenderlich 的网站上发布了更具描述性且更易于遵循的教程:

http://www.raywenderlich.com/4680/how-to-make-an-interface-with-horizontal-tables-like-the-pulse-news-app-part-1

http://www.raywenderlich.com/4723/how-to-make-an-interface-with-horizontal-tables-like-the-pulse-news-app-part-2

  1. 创建常规 UITableView
  2. 创建自定义 UITableViewCell
  3. 添加一个旋转的 UITableView 作为 UITableViewCell 的 subview
  4. 为我们的文章创建另一个自定义 UITableViewCell
  5. 旋转我们的自定义单元格并将其用于水平表格 View

虽然教程是 2011 年的,但有些评论是最近才发表的,因此该方法肯定仍然有效。

还有一个 github 项目引用了今年早些时候的堆栈溢出讨论:

https://github.com/hefgi/TableViewInTableViewCell

如果您打开该项目,iPhone 的 Storyboard 文件 (Main_iPhone.storyboard) 会很有指导意义:

enter image description here

关于ios - 具有多个 TableView 的 Swift MVC 设计,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26302834/

相关文章:

swift - 基于种子 Swift 3 生成随机值

ios - uipagecontrol 当前不动

ios - 从数据 block 播放视频

ios - 使用自动布局约束实现百分比重叠

ios - 使用数据模型正确解析嵌套的 JSON 以进行多次重用。 swift

ios - 'UIButton' 没有可见的@interface 声明选择器 'setTitle'

c++ - 哪种编程技术最能帮助您在错误投入生产之前避免或解决错误

ruby-on-rails - 如何处理 Rails 应用程序中重用的对象

java - 高可用性和灾难恢复软件反模式

ios - 来自外部脚踏开关的 Cordova 应用程序按键事件在 iOS 上不起作用