显示 TableView 部分标题和行 ios swift
Posted
技术标签:
【中文标题】显示 TableView 部分标题和行 ios swift【英文标题】:Show TableView Section Headers and Rows ios swift 【发布时间】:2017-11-17 05:57:42 【问题描述】: 我有一个包含对象的数组,我想在标题中显示组名,但在数组中某些组具有相同的名称,现在我想为同一组显示单个标题并创建行。[
"primary_dark_color": "#0d6d9f",
"Groups": "A",
"primary_color": "#03a9f4"
,
"primary_dark_color": "#0d6d9f",
"Groups": "B",
"primary_color": "#03a9f4"
,
"primary_dark_color": "#0d6d9f",
"Groups": "A",
"primary_color": "#03a9f4"
,
"primary_dark_color": "#0d6d9f",
"Groups": "B",
"primary_color": "#03a9f4"
]
【问题讨论】:
【参考方案1】:要解决您的问题,您需要使用谓词。使用谓词,您可以创建包含相同组的字典组(数组)
试试下面的代码。
let array = [
[
"primary_dark_color": "#0d6d9f",
"Groups": "A",
"primary_color": "#03a9f4"
],
[
"primary_dark_color": "#0d6d9f",
"Groups": "B",
"primary_color": "#03a9f4"
],
[
"primary_dark_color": "#0d6d9f",
"Groups": "A",
"primary_color": "#03a9f4"
],
[
"primary_dark_color": "#0d6d9f",
"Groups": "B",
"primary_color": "#03a9f4"
]]
var arrGroups = [String]()
for dict in array
if !arrGroups.contains(dict["Groups"]!)
arrGroups.append(dict["Groups"]!)
var arrTmp = [Array<Dictionary<String, String>>]()
for i in 0..<arrGroups.count
var tmp:Array = array
tmp = tmp.filter ($0["Groups"]!).range(of: arrGroups[i], options: [.diacriticInsensitive, .caseInsensitive]) != nil
arrTmp.append(tmp)
print(arrTmp)
arrTmp 中的每个数组都有包含相同组的字典
所以你的numberOfSections = arrTmp.count
还有numberOfRowsInSection = arrTmp[section].count
arrTmp 会有
[[["primary_color": "#03a9f4", "primary_dark_color": "#0d6d9f", "Groups": "A"], ["primary_color": "#03a9f4", "primary_dark_color": "#0d6d9f", "Groups": "A"]], [["primary_color": "#03a9f4", "primary_dark_color": "#0d6d9f", "Groups": "B"], ["primary_color": "#03a9f4", "primary_dark_color": "#0d6d9f", "Groups": "B"]]]
【讨论】:
谢谢@Priya 你能告诉我数组值吗以上是关于显示 TableView 部分标题和行 ios swift的主要内容,如果未能解决你的问题,请参考以下文章
iOS 使用 NSDictionary 将数据加载到部分和行中