在分组模式下,UITableView的数据结构将是什么? [英] What would be a good data structure for UITableView in grouped mode
问题描述
通常,我将数据存储在数组中.然后,当调用cellForRowAtIndexPath时,我只需查看行并根据行和过程在数组上选择一个项目即可.
但是我们知道的UITableView可以进行组视图.
那我该怎么办?
我应该有一个数组数组吗?数组的NSDictionary?在UITableView结构中存储数据的最优雅的方法是什么?
例如字典数组,其中每个字典都包含 标题和一个部分的所有项目:
NSArray *dataSource = @[
@{@"title": @"Section 0",
@"rows" : @[ item00, item01, item02] },
@{@"title": @"Section 1",
@"rows" : @[ item10, item11, item12] },
@{@"title": @"Section 2",
@"rows" : @[ item20, item21, item22] },
];
项目可以是字符串或自定义类的对象.那么你就可以 像
NSArray *dataSource = @[
@{@"title": @"Section 0",
@"rows" : @[ item00, item01, item02] },
@{@"title": @"Section 1",
@"rows" : @[ item10, item11, item12] },
@{@"title": @"Section 2",
@"rows" : @[ item20, item21, item22] },
];
一样访问cellForRowAtIndexPath
中的每个项目
Item *item = dataSource[indexPath.section][@"rows"][indexPath.row];
以及所有其他数据源方法也很容易实现.
Usually I store data in an array. Then, when cellForRowAtIndexPath is called I just look at row and select an item on the array based on row and process.
But UITableView as we know can do group view.
So what should I do?
Should I have an array of array? An NSDictionary of array? What would be the most elegant way to store data in UITableView structure?
For example an array of dictionaries, where each dictionary holds the title and all items of one section:
NSArray *dataSource = @[
@{@"title": @"Section 0",
@"rows" : @[ item00, item01, item02] },
@{@"title": @"Section 1",
@"rows" : @[ item10, item11, item12] },
@{@"title": @"Section 2",
@"rows" : @[ item20, item21, item22] },
];
The items can be strings or objects of a custom class. Then you can
access each item in cellForRowAtIndexPath
like
Item *item = dataSource[indexPath.section][@"rows"][indexPath.row];
and all other data source methods are also easily implemented.
这篇关于在分组模式下,UITableView的数据结构将是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!