ios - 带有部分和搜索字段的 UITableView

标签 ios objective-c swift uitableview

我有带 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。 顺便说一句,我只是建议你一个想法,但我认为这是最好的方法。

这是关于我的思维的测试。它工作正常。

enter image description here

enter image description here

这是共享我的项目的 URL。 click here

关于ios - 带有部分和搜索字段的 UITableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42601156/

相关文章:

ios - 子类化 MKAnnotation 错误,符合协议(protocol)

ios - 从 AudioQueueBufferRef 输出队列回调中获取音频 float

android - 为 Android 和 iOS 开发简单视频聊天应用程序的最佳方式

ios - 如何从一对多关系中删除所有托管对象?

ios - 排序描述符中的 @count 在 Objective-C 中给出 'not key value compliant' 错误

iphone - 适用于 iOS 4.3 的 xcode5

iOS7 Storyboard图像选择器不工作 :(

objective-c - NSApplication endSheet : has no effect

iOS swift 标签栏的奇怪行为

ios - 多行文本标签不在 Swift 中换行