【问题标题】:How to deal with dynamic Sections and Rows in iOS UITableView如何处理 iOS UITableView 中的动态 Sections 和 Rows
【发布时间】:2016-10-19 10:14:03
【问题描述】:

我的问题

当我处理UITableView 时,我基本上有一个数组table_data,其中包含我的部分和行。

[
   {
      title : "section A title",
      rows: [
          {data: row_1_data},
          {data: row_2_data}
      ]
   },
   {
      title : "section B title",
      rows: [
          {data: row_1_data},
          {data: row_2_data}
      ]
   },
]

我使用heightForHeaderInSectioncellForRowAtindexPathtitleForHeaderInSectiondidSelectRowAtindexPath 这样的方法

 if indexPath.section == 0 {
        //section A
        if indexPath.row == 0 {
             //do something with table_data[0]["rows"][0]["data"]
        }
        elesif indexPath.row == 1 {
             //do something else
        }
 }
 if indexPath.section == 1 {
      //do something for section B
 }

当我的table_data 数组是动态的时,处理数字01 等会让人头疼。动态意味着某些部分或行可以具有不同的位置或根本不存在。

例如,我删除A部分,我的数组是

[
   {
      title : "section B title",
      rows: [
          {data: row_1_data},
          {data: row_2_data}
      ]
   }
]

我喜欢这个

self.section_A_position = 0
self.section_B_position = 1
func afterRemoveSectionA() {
    section_A_position = -999
    section_B_position = section_B_position - 1
    //write here another new sections for -1 
}

将另一个部分 C 添加为 0 元素

self.section_C_position = -999
func afterAddSectionC() {
   section_C_position = 0
   section_A_position = section_A_position + 1
   section_B_position = section_B_position + 1
}

然后在函数中使用section_positions

 if indexPath.section == section_A_position {
        //section A
        if indexPath.row == 0 {
             //do something with table_data[0]["rows"][0]["data"]
        }
        elesif indexPath.row == 1 {
             //do something else
        }
 }
 if indexPath.section == section_B_position {
      //do something for section B
 }

看起来很简单,但是当我有很多部分和很多案例要隐藏或移动它时,很难控制和添加新类型的部分。有时我会创建section_positions_map 数组将其存储在一起,并在循环中进行+1-1 操作。但这无济于事,当我需要这种行为时,在每个 TableViewController 中组织它仍然很困难。

问题

您知道使这部分变得更容易的任何方法或框架吗?

我的想法

  1. type 属性添加到我的字典中
{
  title : "section A title",
  rows: [
      {data: row_1_data},
      {data: row_2_data}
  ]
  type : "section_a"
}

并检查if table_data[0]["type"] == "section_a"(或使用enum 收集类型)

  1. 子类化我的字典并检查

if table_data[0] is SubClassSectionA

但我觉得他们两个都很丑。

【问题讨论】:

    标签: ios swift xcode uitableview sections


    【解决方案1】:

    我在创建一个我知道所有可能部分的表时使用的一种方法是使用枚举。您为每个可能的部分类型创建一个枚举:

    enum SectionTypes { case sectionA, sectionB, sectionC }

    然后创建一个变量来保存这些部分:

    var sections: [SectionTypes]

    当您准备好数据后,您可以使用需要显示的部分填充部分。我通常也会制作一种方法来帮助获取该部分:

    func getSection(forSection: Int) -> SectionTypes {
            return sections[forSection]
        }
    

    有了这个,你就可以开始实现常见的 DataSource 委托方法了:

    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return sections.count
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        switch getSection(forSection: section) {
        case .sectionA:
            return 0 //Add the code to get the count of rows for this section
        case .sectionB:
            return 0
        default:
            return 0
        }
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        switch getSection(forSection: indexPath.section) {
        case .sectionA:
            //Get section A cell type and format as your need
        //etc
        }
    }
    

    【讨论】:

    • 如果枚举类型为Int你可以使用SectionTypes(rawValue: indexPath.section)
    【解决方案2】:

    使用 NSDictionary 存储数据是一种不好的做法。为部分(即 SectionData)和行(即 RowData)创建新类。您的 ViewController(或其他一些数据提供者)将保留 SectionData 数组,其中 SectionData 保留 RowData 数组。 RowData 可能包含用于处理行选择的函数,因此在 didSelectRowAtindexPath 中您只需执行以下操作:

    let rowData = rowDataAtIndexPath(indexPath)
    rowData.tapHandler()
    

    其中 rowDataAtIndexPath 是您定义的函数,它为您提供 indexPath 的数据。

    当您需要删除某些部分时,只需将其从部分数组中删除并重新加载 tableView。

    【讨论】:

    • 感谢您的回答。这是一种有趣的方式。但是,如果我在一个部分中对 rowData 有不同的行为怎么办?
    • 函数 rowData.tapHandler 对于每个行对象可以不同。你可以放任何你想要的东西。
    【解决方案3】:

    我认为这个谈话比你所寻求的更广泛,但他谈到了它。

    https://realm.io/news/altconf-benji-encz-uikit-inside-out-declarative-programming/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多