【发布时间】: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}
]
},
]
我使用heightForHeaderInSection、cellForRowAtindexPath、titleForHeaderInSection、didSelectRowAtindexPath 这样的方法
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 数组是动态的时,处理数字0、1 等会让人头疼。动态意味着某些部分或行可以具有不同的位置或根本不存在。
例如,我删除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 中组织它仍然很困难。
问题
您知道使这部分变得更容易的任何方法或框架吗?
我的想法
- 将
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 收集类型)
- 子类化我的字典并检查
if table_data[0] is SubClassSectionA
但我觉得他们两个都很丑。
【问题讨论】:
标签: ios swift xcode uitableview sections