- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我的问题
当我处理 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
来收集类型)
如果 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/
这个问题在这里已经有了答案: Multi level categories with items on all levels in UITableView (1 个回答) 关闭 6 年前。 标题说明
我想在编译时从代码中删除未使用的函数。然后我写一些代码(main.c): #include const char *get1(); int main() { puts( get1() );
升级到 Xcode 4.4 后,当我尝试在 iPhone 或 iPad 上运行我的(通用)应用程序时,我遇到了这个烦人的链接器错误。如果我在 iOS 模拟器上运行它,我没有问题... 我尝试添加 ar
我以前从未见过这个错误,我搜索了它可能发生的原因,但找不到任何相关信息: CoreData: error: Serious application error. An exception was ca
我正在使用以下内容填充 UITableView 部分的各个部分: fetchedResultsController = NSFetchedResultsController(fetchReques
是标签允许包含在另一个 中标签?它会在 HTML5 中验证吗? 最佳答案 Yes ! w3 鼓励您在分节内容的元素中显式包装节,而不是依赖于在分节内容的一个元素中包含多个标题而生成的隐式节,并且允许
我使用部分来确定页面的不同部分。这些部分的底部边距为 90px,但是当在页面中创建/放置 ID 为“clients-full”的部分时,我希望出现在它之前/上方的任何部分的底部边距为 0。我可以做吗这
我现在有一个第三方提供的c静态库(用arm-gcc编译的)。我不可能(让第三方)重新编译库。 在调查库内容时,我发现 gcc 选项 -ffunction-sections 和 -fdata-secti
大家好,我陷入了一个奇怪的问题,我正在重新加载 tableViewSection,它正在正确地重新加载该部分,但它隐藏了其他部分,直到我向上或向下滚动它。我使用以下代码重新加载 func tabl
在我的项目中,我有扩展 ArrayAdapter 的类并实现 SectionIndexer .实现方法时getPositionForSection和 getSectionForPosition我发现了
我正在尝试编译 scss,以便在它具有 class1 时获得该部分。我不知道如何去做,因为我得到了空间,而且它的工作方式不一样。 当前输出: .class2 > section .class1 {
我正在阅读 Mark Pilgirm 的“Dive into HTML5”和 semantics section ,它讨论了 HTML5 如何引入 和 元素。它说 s 代表通用文档或部分,而 s
我是使用ARM编译器进行Keil Microvision V5.12编程的新手。 我无法编译一个简单的组装项目,但出现此错误: .\Objects\learn.sct(7): error: L6236
这是我第一次在这里问问题,但我不得不说这个网站在过去几个月里给了我巨大的帮助(iphone-dev-wise),我为此表示感谢。 但是,我没有找到解决这个问题的方法:我有一个包含 2 个部分的 UIT
我在 IOS 中实现了一个 TableView (4 个部分)。问题是我刚刚在第一部分添加了一个部分标题。其他部分没有标题。第一部分没有行(行数为 0)。其他部分有多行。当我滚动时,第一部分的标题不粘
我有一个包含多个部分的表格 View 。我希望能够将行从一个部分移动到另一个部分,并在没有行时删除一个部分。我正在尝试通过 moveRowAtIndexPath 执行此操作,但我的代码不起作用并抛出
我正在尝试删除不符合 for 循环条件的行。但是,我收到错误消息:“尝试从第 1 部分删除第 0 行,但更新前只有 1 个部分。”我以前从未见过这个,也不确定为什么会收到它。 我的代码: func
GCC 页面中针对功能部分和数据部分选项的以下内容: -ffunction-sections -fdata-sections Place each function or data item into
我想在安装新数据库之前删除旧数据库,以便为用户更新它。 我有以下情况: 在我的 Components 部分中,我为用户提供了一个选项: [Components] Name: "updateDataba
我从工作区添加了一个测试用例。可能已经一周了,所以我不记得它是来自 Backlog 还是来自董事会。 现在,当我进入测试/测试计划时,它没有出现。我找不到将其添加到测试套件的方法。 我可以通过测试区域
我是一名优秀的程序员,十分优秀!