如何处理iOS UITableView中的动态节和行 [英] How to deal with dynamic Sections and Rows in iOS UITableView
问题描述
我的问题
当我处理UITableView
时,基本上我的节和行都有一个数组table_data
.
When I deal with UITableView
, I basically have an Array table_data
with my sections and rows.
[
{
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
这样的方法
I use heightForHeaderInSection
, cellForRowAtindexPath
, titleForHeaderInSection
, didSelectRowAtindexPath
methods like this
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
等变得头疼.动态意味着,某些节或行可以具有不同的位置,或者根本不存在.
Working with numbers 0
, 1
, etc becomes a headache, when my table_data
array is dynamic. Dynamic means, that some sections or rows can have different positions or don't exist at all.
例如,我删除A节,而我的数组是
For example, I remove A section and my array is
[
{
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元素
Add another section C as 0 element
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中组织它.
It looks pretty simple, but when I have many sections and many cases to hide or move it in array, it becomes difficult to control and add new types of sections. Sometimes I create section_positions_map
array to store it together and make +1
, -1
operation in loop. But it doesn't help it is still difficult to organize it in every TableViewController, when I need this behavior.
问题
您知道有什么方法或框架可以简化此部分吗?
Do you know any approaches or frameworks that make this part easier?
我的想法
- 将
type
属性添加到我的字典
- Add
type
property to my Dictionaries
{
title : "section A title",
rows: [
{data: row_1_data},
{data: row_2_data}
]
type : "section_a"
}
并检查if table_data[0]["type"] == "section_a"
(或使用enum
收集类型)
and check if table_data[0]["type"] == "section_a"
(or use enum
to collect types)
- 对我的词典进行子类化并检查
if table_data[0] is SubClassSectionA
但是他们两个对我来说都很难看.
but both of them looks ugly for me.
推荐答案
我在创建表时使用的一种方法,在该表中我知道所有可能的部分都包含枚举.您为每种可能的部分类型创建一个枚举:
An approach I use when creating a table where I know all the possible sections is with enums. You create an enum for each of the possible section types:
enum SectionTypes { case sectionA, sectionB, sectionC }
然后创建一个变量来保存这些部分:
And then create a variable to hold the sections:
var sections: [SectionTypes]
准备好数据后,将用需要显示的部分填充各部分.我通常还会提供一种方法来帮助您获取本节:
When you have your data ready then you populate sections with the sections that needs to be displayed. I usually also make a method to help get the section:
func getSection(forSection: Int) -> SectionTypes {
return sections[forSection]
}
有了这个,您就可以开始实现常见的DataSource委托方法:
With this in place you can start implementing the common DataSource delegate methods:
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 UITableView中的动态节和行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!