我有带 TableView 和 SearchBar 的 ViewController。 在 TableView 中有包含用户联系人的单元格。
所有联系人都存储在我从 JSON 中获取的数组中。还有单独的数组用于搜索。当用户在 SearchBar 中键入内容时 - 我使用 SearchBar TextField 中的字符串过滤我的完整联系人数组。一切看起来都很简单。
但是我的应用突然添加了一些新功能 - 部分。 以前我得到了带有用户数组的 JSON,并且我得到了每个用户的姓名和电话。但要知道我还有部分数组,其中包含部分名称和每个部分中的项目数。
之前
{
users: [
{
name: "Jack",
phone: "123-456-78"
},
{
name: "John",
phone: "768-789-98"
},
{
name: "Kate",
phone: "123-879-001"
}
]
}
之后
{
users: [
{
name: "Jack",
phone: "123-456-78"
},
{
name: "John",
phone: "768-789-98"
},
{
name: "Kate",
phone: "123-879-001"
}
],
sections: [
{
title: "Section 1",
itemsCount: 2
},
{
title: "Section 2",
itemsCount: 1
}
}
在这种情况下,向我的代码添加部分的最佳方法是什么?或者我应该考虑从服务器重组数据结构?
最佳答案
如果我需要达到你想要的效果,我想我会像这样从服务器重新组织数据结构:
dataArray = [
{
title:"section1",
items:[
{
name: "Jack",
phone: "123-456-78"
},
{
name: "John",
phone: "768-789-98"
},
{
name: "Kate",
phone: "123-879-001"
}
]
},
{
title:"section2",
items:[
{
name: "Jack",
phone: "123-456-78"
},
{
name: "John",
phone: "768-789-98"
},
{
name: "Kate",
phone: "123-879-001"
}
]
}
]
然后在 iOS 中,如果我想要部分的 items.count,我只需要 dataArray[index][@"items"].count
。
顺便说一句,我只是建议你一个想法,但我认为这是最好的方法。
这是关于我的思维的测试。它工作正常。
这是共享我的项目的 URL。 click here
关于ios - 带有部分和搜索字段的 UITableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42601156/