开发者

Grouping data in an NSMutable array

开发者 https://www.devze.com 2022-12-27 15:06 出处:网络
I have an array I load with lots of data from an xml file. I am displaying this on a tableview and then when you click on say an author it goes through to display all their books.

I have an array I load with lots of data from an xml file. I am displaying this on a tableview and then when you click on say an author it goes through to display all their books.

At the minute my NSMutableArray has an entry for every each book with title, author and so on. So in the table of authors it display the authors name as many times as they have books. I want to group the array data so the table only displays each author once but the author contains an ar开发者_高级运维ray of his books. Seems simple but I can't find a group method for arrays.


You can loop through the books array and create a new array for the authors. For each book, check if that author is already in the authors array using containsObject. But then you'd also have to worry about only showing that author's books from the books array when the user selects an author.

It's probably better to load the xml file into an NSMutableDictionary in the first place instead where the keys are the author names and the object for each key is an NSMutableArray of that author's books.

Then the author tableview would use the dictionary's keys as the data source (using array returned by allKeys which you can then sort if needed or use keysSortedByValueUsingSelector) and the books tableview would use the book array of the selected key.

If the data becomes too large to load into memory all at once, then you'll have to use other options such as SQLite.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号