ios - 如何对 TitleForHeaderInSection 上的日期进行排序

标签 ios objective-c json uitableview sorting

我有如下程序:..- 但是,我想在 TitleForHeaderInSection 中按日期降序排列数据,并且还想将标题中的日期格式化为

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]];
//    [formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setDateFormat:@"EEE, d MMM yyyy"];
NSDate *headerDate = (NSDate *)[managedObject valueForKey:@"dateCreated"];
NSString *headerTitle = [formatter stringFromDate:headerDate];

代码如下:

- (void)viewDidLoad {
    [super viewDidLoad];

    [self.navigationItem setTitle:@"List of Items"];

    NSURL *serverURL = [NSURL URLWithString:SERVER_URL];
    [[DataManager sharedInstance] loadData:serverURL withCompletion:^(NSArray *itemListArray, NSError *error) {
        if (error != nil) {
            UIAlertView *errorAlertView = [[UIAlertView alloc] initWithTitle:@"Server Error" message:@"Unable to fetch Data from Server" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
            [errorAlertView show];
        }
        else {
            fetchData = [self convertSectionTableData:itemListArray keyString:@"date"];
            [self.listTableView reloadData];
        }
    }];
    [self.listTableView reloadData];}


- (NSMutableDictionary *)convertSectionTableData:(NSArray *)convertDataSet keyString:(NSString *)keyString {
    NSMutableDictionary *outputData = [NSMutableDictionary dictionary];
    NSMutableArray *temp = [NSMutableArray array];
    NSString *key = NULL;
    for (NSDictionary *dic in convertDataSet) {

        if (key == NULL) {
            key = [dic objectForKey:keyString];
        } else if (key != [dic objectForKey:keyString]) {
            [outputData setValue:temp forKey:key];
            temp = [NSMutableArray array];

            key = [dic objectForKey:keyString];
        }

        if ([[dic objectForKey:keyString] isEqualToString: key]) {
            [temp addObject:dic];
        }

        if (dic == [convertDataSet lastObject]) {
            [outputData setValue:temp forKey:key];
        }
    }

    return outputData;}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [fetchData count];}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [[fetchData allValues][section] count];}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"CustomListTableCell";

    CustomListTableCell *cell = (CustomListTableCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    int section = (int)indexPath.section;
    int row = (int)indexPath.row;

    NSDictionary *data = [[fetchData allValues][section] objectAtIndex:row];

    cell.homeLabel.text = [data objectForKey:@"home"];

    return cell;}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return [fetchData allKeys][section];}

@end

最佳答案

基本思想是字典是无序的,因此您需要某种方法以正确的顺序检索它们。我可能会建议构建字典键的排序数组。

// build dictionary of objects, keyed by date

NSMutableDictionary *objectsForDates = ...

// build sorted array of dates in descending order

NSArray *dates = [[objectsForDates allKeys] sortedArrayUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
    return [obj2 compare:obj1];
}];

然后您可以使用此 dates 对象来表示 TableView 的“部分”,然后使用它来了解要返回字典中的哪个条目:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [self.dates count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.objectsForDates[self.dates[section]] count];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return [self.formatter stringFromDate:self.dates[section]];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    Location *object = self.objectsForDates[self.dates[indexPath.section]][indexPath.row];
    cell.textLabel.text = object.home;
    return cell;
}

请注意,我建议对您的代码进行一些不相关的更改:

  1. 我建议对您的 JSON 内容使用自定义对象类型。这提供了比简单的 NSDictionary 更强的类型。

    我还要确保类型的类型更自然(例如 id 看起来应该是 NSIntegerdate 看起来应该是 NSDate)。

    我还会为这个自定义类型提供一个 initWithDictionary 初始化程序,以简化解析代码。

  2. 构建按日期键控的字典(您的 convertSectionTableData)的逻辑可以稍微简化。

  3. 您的 UI 日期格式化程序不应使用 en_USlocale。解析 JSON 的格式化程序应该(或者更准确地说,它应该使用 en_US_POSIX),但是在 UI 中呈现格式时,您应该使用用户自己的语言环境。

    您的 UI 日期格式化程序也不应该使用固定的 dateFormat 字符串。使用预先存在的 dateStyle 之一,或者如果您必须使用 dateFormat,请使用 dateFormatFromTemplate 构建本地化版本。

无论如何,把它们放在一起,你会得到类似的东西:

@interface Location : NSObject
@property (nonatomic) NSInteger identifier;
@property (nonatomic, copy) NSString *home;
@property (nonatomic, strong) NSDate *date;

- (instancetype)initWithDictionary:(NSDictionary *)dictionary;
@end

@implementation Location

- (instancetype)initWithDictionary:(NSDictionary *)dictionary {
    self = [super init];
    if (self) {
        self.identifier = [dictionary[@"id"] integerValue];
        self.home = dictionary[@"home"];
        [self setDateFromString:dictionary[@"date"]];
    }
    return self;
}

- (void)setDateFromString:(NSString *)string {
    static NSDateFormatter *formatter;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        formatter = [[NSDateFormatter alloc] init];
        formatter.locale = [NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"];
        formatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
        formatter.dateFormat = @"yyyy-MM-dd";
    });

    self.date = [formatter dateFromString:string];
}
@end

@interface ViewController ()

@property (nonatomic, strong) NSMutableDictionary *objectsForDates;
@property (nonatomic, strong) NSArray *dates;

@property (nonatomic, strong) NSDateFormatter *formatter;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    // set formatter for output

    self.formatter = [[NSDateFormatter alloc] init];
    [self.formatter setDateFormat:[NSDateFormatter dateFormatFromTemplate:@"EEEdMMMyyyy" options:0 locale:[NSLocale currentLocale]]];
    self.formatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];

    // perform request

    NSURL *url = [NSURL URLWithString:@"http://borindatabase.000webhostapp.com/jsonData.php"];
    [[[NSURLSession sharedSession] dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        if (error || !data) {
            NSLog(@"networkError: %@", error);
            return;
        }

        NSError *parseError;
        NSArray *values = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError];
        if (![values isKindOfClass:[NSArray class]]) {
            NSLog(@"parseError: %@", parseError);
        }

        // build dictionary of objects, keyed by date

        NSMutableDictionary *objectsForDates = [[NSMutableDictionary alloc] init];
        for (NSDictionary *value in values) {
            Location *object = [[Location alloc] initWithDictionary:value];
            NSMutableArray *objects = objectsForDates[object.date];
            if (!objects) {
                objects = [[NSMutableArray alloc] init];
                objectsForDates[object.date] = objects;
            }
            [objects addObject:object];
        }

        // build sorted array of dates in descending order

        NSArray *dates = [[objectsForDates allKeys] sortedArrayUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
            return [obj2 compare:obj1];
        }];

        // now update UI

        dispatch_async(dispatch_get_main_queue(), ^{
            self.objectsForDates = objectsForDates;
            self.dates = dates;
            [self.tableView reloadData];
        });

    }] resume];

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [self.dates count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.objectsForDates[self.dates[section]] count];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return [self.formatter stringFromDate:self.dates[section]];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    Location *object = self.objectsForDates[self.dates[indexPath.section]][indexPath.row];
    cell.textLabel.text = object.home;
    return cell;
}

@end

关于ios - 如何对 TitleForHeaderInSection 上的日期进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44877601/

相关文章:

iphone - 自定义 UITextView 占位符在键盘输入时不会删除

iphone - 定义相同功能的两个委托(delegate)?

ios - 如何清除 Objective-C 中的所有 NSUserDefaults 值?

ios - 如何在表格 View 中添加搜索栏

ios - Facebook Connect 始终仅在 iPad 上将应用程序返回到初始屏幕

ios - UICollectionView 错误 : cells must be retrieved by calling -: -dequeueReusableCellWithReuseIdentifier:forIndexPath:

Python 数组按单词过滤

ios - 来自 UIAlertView _performPopup 的低级 iOS 崩溃

javascript - 为什么在这种情况下我不能过滤本地 json 文件 (vuejs2)

java - jsonbin.io 获取 Json 数据滞后问题