我正在从服务器获取表数据。它是一个 JSON 数据,每个数组都保存为字典。我有一组 JSON 对象。
JSON是:
{
sAlbum = Fallen;
sArtist = Evanescence;
sId = 1;
sRate = 3;
stitle = "Everybody's Fool";
},
{
sAlbum = Fallen;
sArtist = Evanescence;
sId = 2;
sRate = 4;
stitle = "Going Under";
}
当我拥有它时,我会使用(使用 AFnetwrking)
[AFJSONRequestOperation JSONRequestOperationWithRequest:request
// The success block runs when (surprise!) the request succeeds.
success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
self.musicLibraryArr = [(NSDictionary*)JSON objectForKey:@"array"];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"stitle" ascending: YES];
NSArray *sortedArray = [self.musicLibraryArr sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
self.musicLibraryArr = [[NSMutableArray alloc] initWithArray:sortedArray];
self.loadingView.hidden = YES;
// partition
self.tableData = [self partitionObjects:self.musicLibraryArr collationStringSelector:@selector(self)];
NSLog(@"HEADER: %@", self.tableData);
[self.tableView reloadData];
}
现在我打电话给 self.tableData = [self partitionObjects:self.musicLibraryArr collationStringSelector:@selector(self)]; 为了创建节数组
但是我得到了从 A 到 Z 的部分,并且在每个部分中我都有所有 JSON 数据(数据在每个部分中重复,而不是将数据拆分为部分)。
分区方法是:
-(NSArray *)partitionObjects:(NSArray *)array collationStringSelector:(SEL)selector
{ UILocalizedIndexedCollation *collation = [UILocalizedIndexedCollation currentCollation]; NSInteger sectionCount = [[collation sectionTitles] count]; NSMutableArray *unsortedSections = [NSMutableArray arrayWithCapacity:sectionCount];
for (int i = 0; i < sectionCount; i++) {
[unsortedSections addObject:[NSMutableArray array]];
}
for (id object in array) {
NSInteger index = [collation sectionForObject:[object objectForKey:@"sArtist"] collationStringSelector:selector];
[[unsortedSections objectAtIndex:index] addObject:object];
}
NSMutableArray *sections = [NSMutableArray arrayWithCapacity:sectionCount];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"sArtist" ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)];
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
for (NSMutableArray *section in unsortedSections) {
NSArray *sortedArray = [section sortedArrayUsingDescriptors:sortDescriptors];
// collationStringSelector:selector]]; [部分 addObject:sortedArray]; }
return sections;
}
编辑 - #2 我在这里初始化了数据:
AFJSONRequestOperation *operation =
[AFJSONRequestOperation JSONRequestOperationWithRequest:request
// The success block runs when (surprise!) the request succeeds.
success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
self.noMusicView.hidden = YES;
// 从json数组中获取数据数组
self.musicLibraryArr = [(NSDictionary*)JSON objectForKey:@"array"]; NSLog(@"JSON 数据:%@", self.musicLibraryArr); // 排序数组
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"stitle" 升序:YES]; NSArray *sortedArray = [self.musicLibraryArr sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
self.musicLibraryArr = [[NSMutableArray alloc] initWithArray:sortedArray];
self.loadingView.hidden = YES;
self.indexPathController.dataModel = [[TLIndexPathDataModel alloc] initWithItems:self.musicLibraryArr andSectionNameKeyPath:@"sTitle" andIdentifierKeyPath:@"sId"];
//HERE I GET NULL (我检查是否可以在 NSLog 中看到数据)
NSLog(@"DATA MODEL %@", self.indexPathController.dataModel);
[self.tableView reloadData];
}
// The failure block runs if something goes wrong, such as when the network isn’t available. If that happens, you display an alert view with an error message.
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
// Error view
self.noMusicView.hidden = NO;
self.loadingView.hidden = YES;
self.noMusicViewLabel.text = error.localizedDescription;
}];
可能我在 partitionObjects 方法中遗漏了一些东西,请帮忙