1

我正在从服务器获取表数据。它是一个 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 collat​​ionStringSelector:@selector(self)]; 为了创建节数组

但是我得到了从 A 到 Z 的部分,并且在每个部分中我都有所有 JSON 数据(数据在每个部分中重复,而不是将数据拆分为部分)。

分区方法是:

-(NSArray *)partitionObjects:(NSArray *)array collationStringSelector:(SEL)selector

{ UILocalizedIndexedCollat​​ion *collat​​ion = [UILocalizedIndexedCollat​​ion currentCollat​​ion]; NSInteger sectionCount = [[collat​​ion 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];

// collat​​ionStringSelector: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 方法中遗漏了一些东西,请帮忙

4

1 回答 1

0

您可以使用 TLIndexPathTools在此处尝试解决方案。这种方法使您不必自己操作传入的数据。您只需要提供您的字典数组和一个sectionNameKeyPath. 请参阅示例代码。

编辑根据您在下面的评论,我添加了一个示例项目,该项目演示了一个嵌入在UIViewController对 TLIndexPathTools 的依赖最小的表视图。尝试运行Minimal 项目。视图控制器的源码如下:

#import <UIKit/UIKit.h>

#import "TLIndexPathController.h"

@interface ViewController : UIViewController <TLIndexPathControllerDelegate, UITableViewDataSource>
@property (strong, nonatomic) IBOutlet UITableView *tableView;
@end

#import "ViewController.h"

@interface ViewController ()
@property (strong, nonatomic) TLIndexPathController *indexPathController;
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.indexPathController = [[TLIndexPathController alloc] initWithItems:@[
                                @"Chevrolet",
                                @"Bubble Gum",
                                @"Chalkboard"]
                                ];
}

#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.indexPathController.dataModel.numberOfSections;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.indexPathController.dataModel numberOfRowsInSection:section];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    NSString *title = [self.indexPathController.dataModel itemAtIndexPath:indexPath];
    cell.textLabel.text = title;
    return cell;
}

#pragma mark - TLIndexPathControllerDelegate

- (void)controller:(TLIndexPathController *)controller didUpdateDataModel:(TLIndexPathUpdates *)updates
{
    [updates performBatchUpdatesOnTableView:self.tableView withRowAnimation:UITableViewRowAnimationFade];    
}

@end

以此为基础,您将配置数据模型,如链接的帖子中所示。

编辑#2数据模型配置看起来像这样:

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.indexPathController = [[TLIndexPathController alloc] init];

    //if you get your JSON asynchronously, you would make the request after initializing
    //the index path controller, possibly right here. Assuming your JSON request
    //passes the result back through a callback block, the block would process
    //the incoming array like this block:
    ^(NSArray *jsonData){
        self.indexPathController.dataModel = [[TLIndexPathDataModel alloc] initWithItems:jsonData
                                                                   andSectionNameKeyPath:@"sTitle"
                                                                  andIdentifierKeyPath:@"sId"];
    };

    //or if you already have the data, just set the data model directly:
    self.indexPathController.dataModel = [[TLIndexPathDataModel alloc] initWithItems:jsonData
                                                           andSectionNameKeyPath:@"sTitle"                                                         
                                                           andIdentifierKeyPath:@"sId"];
}

如果您需要动态更新数据,只需如上所示创建一个新的数据模型并dataModel再次设置索引路径控制器的属性。控制器将调用其controller:didUpdateDataModel方法,如上面的示例代码所示,委托将调用performBatchUpdatesOnTableView:更新对象,从而使新数据平滑地动画化。

于 2013-06-23T14:23:47.763 回答