【发布时间】:2017-07-03 03:59:49
【问题描述】:
我的程序如下:..- 但是,我想在 TitleForHeaderInSection 中按日期降序对数据进行排序,并且还想将 Header 中的日期格式化为
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
【问题讨论】:
-
你能添加你的数据格式吗?
-
目前你的代码是什么?我的意思是什么有效,什么无效?
-
这是我要检索的数据:borindatabase.000webhostapp.com/jsonData.php 数据检索得很完美,但排序不正确。而且我不知道如何以如下方式格式化 TitleForHeaderInSection 中的日期:Mon, 3 Jul 2017
标签: ios objective-c json uitableview sorting