如何使用静态单元格支持重新创建 UITableViewController? [英] How to recreate UITableViewController with static cells support?

查看:17
本文介绍了如何使用静态单元格支持重新创建 UITableViewController?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多通用的逻辑,因此我所有的viewController 都继承自BaseViewController、BaseDetailsViewController 或BaseWebViewController.最后两个继承自 BaseViewController,后者继承自 UIViewController.问题是我不能将 tableViews 与静态单元格一起使用,因为我的 BaseDetailsViewController 不是从 UITableViewController 继承的.

I have a lot of common logic, therefore all my viewControllers inherit from BaseViewController, BaseDetailsViewController or BaseWebViewController. The last two inherit from BaseViewController which inherits from UIViewController. The problem is I cannot use tableViews with static cells because my BaseDetailsViewController is not inheriting from UITableViewController.

我从来没有使用和继承过 UITableViewController 因为同样可以通过继承 UIViewController 来实现,将它与添加的 UITableView 连接起来并实现数据源和委托方法.因此,对于表单类型屏幕(注册或其他数据输入屏幕),我使用我自己的 BaseDetailsViewController,它添加了漂亮且方便的方法(输入验证、自定义样式、滚动键盘、处理输入字段导航等)

I'v never used and inherited UITableViewController because the same could be achieved by inheriting from UIViewController, wiring it with an added UITableView and having implemented datasource and delegate methods. Therefore for the form type screens (SignUp or other data input screens), I use my own BaseDetailsViewController which adds nice and handy methods (input validation, custom styling, scrolling keyboard, handles input field navigation and etc.)

使用 UITableViewController 有两个真正的好处:1) 键盘滚动(如果您使用文本字段)和 2) 在屏幕上轻松创建表单 UI 元素.第一个好处对我来说无关紧要,因为我已经在 BaseDetailsViewController 中实现了这些东西.关于第二个好处,我在 scrollView 中创建表单,这提供了很大的灵活性,但需要更多的努力(尤其是在需要更新屏幕时).因此我听说有些人实现了他们自己的 UITableViewController,然后让它继承自他们的 BaseViewController.因此我开始深入研究这种方法.

Using UITableViewController has two real benefits: 1) keyboard scrolling (if you use text fields) and 2) easy creation of your form UI elements inside on screen. The first benefit is irrelevant for me, because I'v already have my own implementation of this stuff inside BaseDetailsViewController. Regarding the second benefit, I'm creating my form inside scrollView which gives a lot of flexibility but it's a little bit more effort (especially when need to update the screen). Therefore I heard some guys implemented their own UITableViewController and then made it to inherit from their BaseViewController. Therefore I started to dig into this approach.

到目前为止,我已经想到了这两种方法:

So far I'v came upon these two approaches:

  1. 重新创建您自己的 tableVC.不清楚如何使其适用于静态单元格.目前,如果我的 ReplaceTableViewController 没有实现 numberOfRows 和 cellForRow,我的演示应用程序会崩溃,如果我使用虚拟内容实现它们,则不会显示静态单元格.
  2. 方法调配.不清楚如何通过为 UITableViewController 注入 baseViewController 继承来改变类继承,想了想还是不清楚需要添加哪些方法.
  1. Recreate your own tableVC. Not clear how to make it work for static cells. Currently my demo app crashes if my ReplaceTableViewController is not implementing numberOfRows and cellForRow, and not shows static cells if I implement them with dummy content.
  2. method-swizzling. Not clear how to change class inheritance by injecting baseViewController inheritance for UITableViewController, thought still not clear which methods are needed to be added.

有人试过想分享吗?

更新 我没有使用故事板,因为我提倡干净的 MVC - 作为组件的每个屏幕都应该有自己的模型、视图 (Xib) 和控制器,所有这些都存储在单独的文件中以消除合并冲突大型应用程序项目(30 多个屏幕)中的多个开发人员.因此,带有嵌入到 tableViewController 的 segue 的容器视图也不是一种选择.

UPDATE I'm not using storyboards as I promote clean MVC - every screen as component should have its own Model, View (Xib), and controller, all stored in separate files to eliminate merge conflicts of multiple devs in large app projects (30+ screens). And therefore container view with embedded segue to tableViewController is also not an option.

UPDATE2 如果有人想了解我这样做的原因和方式,这里是我的开源链接 基于模板的新项目生成器框架,我最近开始发布.

UPDATE2 In case anybody would like to take a look why and how I'm doing this, here's a link to my open source template based new project generator framework which I started to publish recently.

推荐答案

静态单元格需要一个 UITableViewController.您现在无法更改此设置.

Static cells need a UITableViewController. You can't changed this right now.

但是要在 UITableViewController 之外的 UITableView 中使用静态单元格,您可以在 Non-Table-UIViewController 中使用 ContainerView,您可以在其中放置一个真正的、单独创建的 UITableViewController 来处理这些静态单元格.

But to use static cells in a UITableView outside of a UITableViewController you can use a ContainerView in your Non-Table-UIViewController, where you place a real, separate created UITableViewController working with those static cells.

要在多个视图控制器之间共享公共代码,继承并不是最好的解决方案,正如您在继承 UIViewController 与 UITableViewController 时自己发现的那样.您可以使用某种组合来共享代码或 - 特别是对于 Objective-c - 类别.

To share common code between multiple view controllers, inheritance is not that best solution as you found out by yourself while subclassing UIViewController vs. UITableViewController. You can use some sort of composition for sharing code or - especially for objective-c - categories.

类别不允许有自己的属性,但有可能使用 objc_setAssociatedObject 的解决方法.

Categories are not allowed to have its own properties, but there are workarounds possible with objc_setAssociatedObject.

另一种方法是不使用静态单元格,而是使用带有 DataSource-Delegate 的 UITableView 中的动态单元格:)

Another approach would be to not use static cells, but dynamic cells in a UITableView with DataSource-Delegate :)

正如您在我的屏幕截图中看到的,要重用带有静态单元格的特殊 TableView,我将其放置在 ContainerView 中的其他 ViewController 中.由于您没有使用故事板,我很确定这也可以通过没有故事板的代码来完成.

As you see in my screenshot, to reuse a special TableView with static cells, i place it in other ViewControllers in a ContainerView. Since you are not using storyboards, i am pretty sure that this can also be done by code without storyboard.

这篇关于如何使用静态单元格支持重新创建 UITableViewController?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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