自定义编写的分组 UITableView 真的是在 iOS 上实现标准表单的方法吗?
这是什么意思
我注意到我使用的许多应用程序都有类似的实现表单(集)的方式。例如:
这基本上与“设置”应用程序中使用的方案相同。
我的问题
当我需要做这样的事情时,我基本上做了一个分组的 UITableView,定义有多少个组,每个组有多少个字段等。cellForRowAtIndexPath
基本上变成了一个大的 switch 语句,它将正确的表单字段设置为 accesoryView
。我提前在代码中创建了所有表单字段,并确保它们设置正确。
然而,我有一种感觉,一定有更好的方法。这感觉像是有很多重复的代码。我发现很难使代码变得漂亮。
我想要什么
理想情况下,我只想完全从 Interface Builder 编写表单,因为它对我来说非常直观。因此,在 Interface Builder 中创建一个 UITableView
及其内容。当然,在 IB 中制作一个表单很简单,但同时将字段放入分组的表视图中则不然——我确实想要这样,因为它看起来很漂亮且一致。
另一个选项类似于 设置包< /a>:我定义字段的类型和名称,应用程序的代码呈现表格,创建字段实例等。
我正在寻找更好的东西,因为这对我来说似乎很奇怪这样一个极其普遍的问题需要以如此复杂的方式来处理。特别是考虑到很多常见问题已经得到很好的处理。但在这个问题上,我找不到任何东西。
那么:我的方法真的是显示此类表单的最佳方式吗?零件可以改进吗?有没有我忽略的现有 iOS 库可以帮助我的方法?
What is this about
I notice many many apps I use have a similar way of implementing form (sets). For example:
This is basically the same scheme used in the Settings app as well.
My problem
When I need to make something like this, I basically make a grouped UITableView
, define how many groups with how many fields each I have, etc. The cellForRowAtIndexPath
basically becomes a big switch statement which sets the right form field as accesoryView
. I make all the form fields in advance in the code and make sure they are set up properly.
However, I have this feeling that there must be a much better way. This feels like an awful lot of repetitive code. And code which I find very difficult to make pretty.
What I want
Ideally, I'd just like to write my form completely from Interface Builder, because it's really intuitive for me. So creating a UITableView
and it-s content in Interface Builder. Just making a form in IB is simple of course, but not while also putting the fields in a grouped tableview - which I do really want because it looks pretty and consistent.
Another option is something like the Settings bundles: I define the type and name of my fields, the code of the app renders the table, creates the field instances, etc.
I'm in search of something better because it seems quite odd to me that such a extremely common problem needs to be handled in such a complex way. Especially considering that so many of the common problems are already handled well. But on this problem, I can't find anything.
So: is my approach really the best way of displaying these kind of forms? Can parts be improved? Is there any way I've overlooked for the existing iOS libraries to help me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
UITableView
确实是最好的方法。您可以使用UIScrollView
,但这将涉及更多工作,因为您会失去UITableView
提供的内置函数。如果您更喜欢使用 IB(我完全理解),那么您可以在 IB 中创建每个
UITableViewCell
,用对象对其进行布局,链接一个IBOutlet
,然后以这种方式将其包含到您的UITableView
中。这也使得您可以轻松地为您正在使用的对象(例如UITextField
和UISwitch
)设置操作和委托。A
UITableView
really is the best way to go. You could use aUIScrollView
, but that will involve a lot more work since you lose out on the built in functions thatUITableView
offers.If you prefer to use the IB, which I completely understand, then you can create each
UITableViewCell
in the IB, lay it out with objects, link up anIBOutlet
, and then include it that way into yourUITableView
. This also makes it easy to set up actions and delegates for the objects you're using, e.g.UITextField
s andUISwitch
es.