ios - 如何最好地处理 UITableView 子部分?

标签 ios json uitableview

我正在使用来自 Locu 的数据,如下所示:

{
  "menus": [
    {
      "menu_name": "Dinner",
      "sections": [
        {
          "section_name": "Starters",
          "subsections": [
            {
              "subsection_name": "",
              "contents": [
                {
                  "price": "6.00",
                  "type": "ITEM",
                  "name": "Soup of the Day"
                },

等...

当有多个部分(它们直接映射到 UITableView 部分)并且每个部分都有一个包含项目的子部分时,使用此结构实现 UITableView 没有问题。

我遇到的困难是处理某种相反的情况——只有一个部分,但有多个小节。

例子!

menu_name: Dinner
    section0: Starters
        subsection0: ""
            (items)
    section1: Salads
        subsection0: ""
            (items)
    section2: Entrees
        subsection0: ""
            (items)

对比

menu_name: Beer & Wine
    section0: Beers
        subsection0: On Tap
            (items)
        subsection1: Bottled
            (items)

在这些示例中,晚餐菜单可以轻松映射到 UITableView 部分,而 numberOfSectionsInTableView: 是所有部分的简单计数。但是一旦你把啤酒和 Wine 菜单混在一起,假设所有菜单都在一个长 View 中,你如何计算 numberOfSectionsInTableView: 的部分?您如何计算 numberOfRowsInSection: 的行数?另外,我认为 UITableView 甚至不能处理小节,那么你在那里做什么?只显示一个没有行的部分,然后是另一个部分?

最佳答案

你是对的。不真正支持小节。您必须有点创意才能让它按您希望的方式工作。

本质上,您必须“模拟小节”。它们只是像其他行一样的行,但可能会使它们的单元格看起来与其他单元格或类似的东西略有不同,阴影更暗。

然而,它最终需要更多的维护,因为您必须将这些小节作为常规行与其他行一起跟踪,同时试图使它们看起来更像小节。

关于ios - 如何最好地处理 UITableView 子部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21033284/

相关文章:

iphone - UITableView 像 Xcode 中的 gridview 一样吗?

ios - didSelectRowAt indexPath 未被调用

iOS SDK : MapKit MKPolyLine not showing

ios - NSLayoutConstraint 冲突。 UIView-Encapsulated-Layout-Height

ios - 调整 UIImage 高度,同时保持其纵横比(iOS)

python - 如何将 Flask 响应对象转换为字典

json - 网络安全组规则审核 (Azure)

java - 带有 @ 的 JSON token 名称

ios - UiCollcetionViewCell 特定的 segue 连接 TableView 显示 TableView 内的特定内容

ios - YouTube API : clang: error: linker command failed with exit code 1 (use -v to see invocation)