gpt4 book ai didi

ios - 如何处理 iOS UITableView 中的动态 Sections 和 Rows

转载 作者:搜寻专家 更新时间:2023-10-31 22:05:45 24 4
gpt4 key购买 nike

我的问题

当我处理 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. 子类化我的词典并检查

如果 table_data[0] 是 SubClassSectionA

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

最佳答案

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

enum SectionTypes { case sectionA, sectionB, sectionC}

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

可变部分:[SectionTypes]

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

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

有了这个,您就可以开始实现通用的数据源委托(delegate)方法了:

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
}
}

关于ios - 如何处理 iOS UITableView 中的动态 Sections 和 Rows,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40128423/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com