サーバーからテーブル データを取得しています。これは 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 DATA: %@", 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 メソッドに何かが欠けています。助けてください