iOS 使用 NSDictionary 将数据加载到部分和行中
Posted
技术标签:
【中文标题】iOS 使用 NSDictionary 将数据加载到部分和行中【英文标题】:iOS Using NSDictionary to load data into section and rows 【发布时间】:2012-07-26 16:37:20 【问题描述】:我希望在行中显示以日期作为部分标题和书名的表格部分。我完全不知道如何使用 NSDictionary 将数据加载到部分和行中。因此我的代码都搞砸了:(谁能提供一些帮助,以一些示例代码作为指导?我的 NSMutableArray 如下:
(
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
【问题讨论】:
【参考方案1】:要处理您的数据,您需要执行类似的操作
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];
然后在一个section中获取一行
NSString *sectionTitle = // get the section title calling the above code
NSArray *items = [self.dataSource objectForKey:sectionTitle];
return [items objectAtIndex:indexPath.row];
【讨论】:
非常感谢!我使用您的代码作为指导并进行了一些编辑,它完全符合我的要求!!!非常感谢!! :D 对不起,我有一个问题.. UI 结果很好,但是当我为书做续订按钮时,我注意到虽然书是按日期排序的,但按钮是被回收,我不知道如何为续订按钮设置标签。有什么建议吗?:) 你最好的选择是问另一个问题或环顾四周 - 我相信有很多与细胞重用有关的答案【参考方案2】: - Dictionary of sections by date
|
--- array of row objects for the date1
| |
| --- row dictionary with the data to display
| |
| --- row dictionary with the data to display
| |
| --- row dictionary with the data to display
| |
| --- row dictionary with the data to display
|
|
|
--- array of row objects for the date2
| |
| --- row dictionary with the data to display
| |
| --- row dictionary with the data to display
| |
| --- row dictionary with the data to display
|
|
|
|
--- array of row objects for the date3
|
--- row dictionary with the data to display
|
--- row dictionary with the data to display
|
--- row dictionary with the data to display
|
--- row dictionary with the data to display
|
--- row dictionary with the data to display
【讨论】:
谢谢!它帮助我更好地理解 NSDictionary 在做什么!非常感谢! :)【参考方案3】:简而言之,您不能将对象重新添加到 NSDictionary。那是因为 NSDictionary 是不可变的。一旦它被创建,它的内容就不能被修改。你想要的是一个 NSMutableDictionary。
然后您可以使用 setObject:forKey: 方法。
[MyDict setObject:@"Normal Loan" forKey:@"status"];
https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/nsmutabledictionary_Class/Reference/Reference.html
【讨论】:
哦.. 我知道这就是 NSDictionary 和 NSMutableDictionary 之间的区别。想知道为什么.. 感谢您的贡献:)【参考方案4】:正如 A-Live 所说,您必须将数组排序到数组的可变字典中
以下代码应该可以工作,但未经测试
在 TableView 控制器.h
@interface MyController:UITableViewController
NSMutableArray* dict;
NSMutableArray* array;
在 MyController.m 中
-(void)initWithBooksArray:(NSArray*)bookArray //Or something similar
self = [[super alloc] initWithStyle:UITableViewStylePlain];
if(self)
dict = [[NSMutableDictionary alloc] init];
array= [[NSMutableArray alloc] init];
NSMutableArray* sectionArray;
for(NSDictionary* bookDict in bookArray)
date = [bookDict valueForKey:@"date"];
sectionArray = [dict valueForKey:date];
if(sectionArray)
[sectionArray addObject:bookDict];
else
sectionArray = [[NSMutableArray arrayWithObject:bookDict];
[dict addObject:sectionArray forKey:date];
array = [[dict allKeys] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
return self;
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
return [dict count];
-(NSInterger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
NSString* key = [array objectAtIndex:section];
return [[dict objectForKey:key]count];
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
return [array objectAtIndex:section];
...
// I'll leave it to apple's documentation (or someone else) to fill in the blanks on how to create cells
【讨论】:
感谢您的帮助! :) 但我使用了下面的答案。一样的感谢:)以上是关于iOS 使用 NSDictionary 将数据加载到部分和行中的主要内容,如果未能解决你的问题,请参考以下文章
如何在 iOS 中使用 NSDictionary 添加图像并使用 POST 方法将数组列表发送到服务器
在 Push for Segue [IOS] 中获取 NSDictionary 选定行