iOS使用NSDictionary将数据加载到节和行中 [英] iOS Using NSDictionary to load data into section and rows
问题描述
我希望显示包含日期的表格部分作为行中的部分标题和书名。我对于如何使用NSDictionary将数据加载到节和行中感到很遗憾。因此,我的代码都搞砸了:(任何人都可以提供一些帮助,有一些示例代码作为指导吗?我的NSMutableArray如下:
I wish to display table section with date as the section title and name of book in the row. I'm all lost at how to use a NSDictionary to load data into section and rows. Therefore my codes are all screwed up :( Can anyone offer some help, with some sample codes as a guide? My NSMutableArray is as followed:
(
{
BIBNo = 187882;
date = "14/08/2012";
itemSequence = 000010;
name = "Increasing confidence / Philippa Davies.";
noOfRenewal = 0;
number = 000187907;
status = "Normal Loan";
time = "24:00";
type = Loans;
},
{
BIBNo = 291054;
date = "14/08/2012";
itemSequence = 000010;
name = "iPhone application development for IOS 4 / Duncan Campbell.";
noOfRenewal = 2;
number = 000291054;
status = "Normal Loan";
time = "24:00";
type = Loans;
},
{
BIBNo = 244441;
date = "15/08/2012";
itemSequence = 000010;
name = "Coach : lessons on the game of life / Michael Lewis.";
noOfRenewal = 1;
number = 000244441;
status = "Normal Loan";
time = "24:00";
type = Loans;
},
{
BIBNo = 290408;
date = "15/08/2012";
itemSequence = 000010;
name = "Sams teach yourself iPhone application development in 24 hours / John Ray.";
noOfRenewal = 3;
number = 000290408;
status = "Normal Loan";
time = "24:00";
type = Loans;
},
{
BIBNo = 161816;
date = "16/08/2012";
itemSequence = 000010;
name = "Malaysian Q & As / compiled by Survey & Interview Department ; illustrations by Exodus.";
noOfRenewal = 1;
number = 000161817;
status = "Normal Loan";
time = "24:00";
type = Loans;
},
{
BIBNo = 187883;
date = "16/08/2012";
itemSequence = 000010;
name = "Positive thinking / Susan Quilliam.";
noOfRenewal = 3;
number = 000187899;
status = "Normal Loan";
time = "24:00";
type = Loans;
}
)
我如何将对象重新添加到NSDictionary中以便具有相同日期的对象将被存储在一起以显示为该部分中的行?请给我一些建议,谢谢! T_T
How do i actually re-add the objects into NSDictionary so that objects with the same date would be stored together to be displayed as rows in the section? Please offer me some advice thanks! T_T
推荐答案
要处理您的数据,您需要执行以下操作:
To process your data you would need to do something like
NSMutableDictionary *dataSource = [[NSMutableDictionary alloc] init]; // This would need to be an ivar
for (NSDictionary *rawItem in rawItems) {
NSString *date = [rawItem objectForKey:@"date"]; // Store in the dictionary using the data as the key
NSMutableArray *section = [dataSource objectForKey:date]; // Grab the section that corresponds to the date
if (!section) { // If there is no section then create one and add it to the dataSource
section = [[NSMutableArray alloc] init];
[dataSource setObject:section forKey:date];
}
[section addObject:rawItem]; // add your object
}
self.dataSource = dataSource;
然后获取部分标题
NSArray *sections =[[self.dataSource allKeys] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
return [sections objectAtIndexPath:indexPath.section];
然后在一个部分中获得一行
Then to get a row in a section
NSString *sectionTitle = // get the section title calling the above code
NSArray *items = [self.dataSource objectForKey:sectionTitle];
return [items objectAtIndex:indexPath.row];
这篇关于iOS使用NSDictionary将数据加载到节和行中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!