Joh*_*kon 3 xcode uitableview ios sections swift
我的问题
当我处理时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}
]
},
]
Run Code Online (Sandbox Code Playgroud)
我用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
}
Run Code Online (Sandbox Code Playgroud)
与数字打交道0,1等变成了头疼,当我的table_data数组是动态的.动态意味着某些部分或行可以具有不同的位置或根本不存在.
例如,我删除A部分,我的数组是
[
{
title : "section B title",
rows: [
{data: row_1_data},
{data: row_2_data}
]
}
]
Run Code Online (Sandbox Code Playgroud)
我喜欢这个
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
}
Run Code Online (Sandbox Code Playgroud)
将另一个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
}
Run Code Online (Sandbox Code Playgroud)
然后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
}
Run Code Online (Sandbox Code Playgroud)
它看起来很简单,但是当我有许多部分和许多情况要隐藏或移动它的数组时,很难控制和添加新类型的部分.有时候,我创建section_positions_map阵列将其存储起来,并+1,-1操作循环.但是当我需要这种行为时,它仍然很难在每个TableViewController中组织它.
题
您是否知道使这部分更容易的任何方法或框架?
我的想法
type属性到我的词典{
title : "section A title",
rows: [
{data: row_1_data},
{data: row_2_data}
]
type : "section_a"
}
Run Code Online (Sandbox Code Playgroud)
并检查if table_data[0]["type"] == "section_a"(或用于enum收集类型)
if table_data[0] is SubClassSectionA
但是他们两个看起来都很难看.
我在创建表时使用的方法,我知道所有可能的部分都是枚举.您为每个可能的节类型创建枚举:
enum SectionTypes { case sectionA, sectionB, sectionC }
然后创建一个变量来保存部分:
var sections: [SectionTypes]
准备好数据后,可以使用需要显示的部分填充部分.我通常也会制作一个方法来帮助获取该部分:
func getSection(forSection: Int) -> SectionTypes {
return sections[forSection]
}
Run Code Online (Sandbox Code Playgroud)
有了这个,您就可以开始实现常见的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
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3217 次 |
| 最近记录: |