Reputation: 21111
I have ViewController with TableView and SearchBar. In the TableView there are cells with user contacts.
All contacts are stored in array that I take from JSON. There is also separate array for search. When user types something in SearchBar - I filter my full array of contacts using string from SearchBar TextField. Everything looks simple.
But suddenly some new feature was added to my app - sections. Previously I got JSON with array of users and for each of them I got name and phone. But know I also got sections array that have section name and count of items in each section.
Before
{
users: [
{
name: "Jack",
phone: "123-456-78"
},
{
name: "John",
phone: "768-789-98"
},
{
name: "Kate",
phone: "123-879-001"
}
]
}
After
{
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
}
}
What is the best way to add sections to my code in this case? Or I should consider reorganizing data structure from server?
Upvotes: 0
Views: 622
Reputation: 267
if i need to achieve what effort you want, i think i will reorganizing data structure from server just like this :
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"
}
]
}
]
then in iOS, if i want the items.count of section,i only need dataArray[index][@"items"].count
.
by the way, i only suggest your a thinking, but i think this is the best way.
here is the test about my thinking. it works fine.
here is the URL where my project is shared.click here
Upvotes: 1