次のようにやろう(これはあなたの問題を解決することを願っています) -
1-グローバルNSMutableArray
つまりを取りますresultArray。
2-(も動的配列のサイズで動作)、あなたの配列を作るこれらのコード行を追加します -
resultArray = [NSMutableArray new];
NSArray *groupsDate = [yourArray valueForKeyPath:@"@distinctUnionOfObjects.date"];
// for sorted array- [[yourArray valueForKeyPath:@"@distinctUnionOfObjects.date"]sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
for (NSString *groupDateValue in groupsDate)
{
NSMutableDictionary *newDict = [NSMutableDictionary new];
[newDict setObject:groupDateValue forKey:@"date"];
NSArray *groupRate = [yourArray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"date = %@", groupDateValue]];
[newDict setObject:groupRate forKey:@"heartrate"];
[resultArray addObject:newDict];
}
NSLog(@"result %@",resultArray);
今これはtableView
dataSource/Delegate
methods-
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return resultArray.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
NSArray *rowC=[[resultArray objectAtIndex:section]valueForKey:@"heartrate"];
return rowC.count;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 30)];
UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(3, 0, tableView.frame.size.width/2-5, 30)];
[label1 setFont:[UIFont fontWithName:@"HelveticaNeue-Medium" size:16]];
[label1 setTextColor:[UIColor whiteColor]];
[label1 setText:[self getDateFromString:[[resultArray valueForKey:@"date"]objectAtIndex:section]]];
[label1 setTextAlignment:NSTextAlignmentLeft];
[view addSubview:label1];
[view setBackgroundColor:[UIColor darkGrayColor]];
return view;
}
-(NSString *)getDateFromString:(NSString *)string
{
NSString * dateString = [NSString stringWithFormat: @"%@",string];
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss +0000"];
NSDate* myDate = [dateFormatter dateFromString:dateString];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"dd MMM yyyy"];
NSString *stringFromDate = [formatter stringFromDate:myDate];
return stringFromDate;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
cell.textLabel.text=[NSString stringWithFormat:@"Heart Rate = %@",[[[[resultArray objectAtIndex:indexPath.section]valueForKey:@"heartrate"] valueForKey:@"heartrate"] objectAtIndex:indexPath.row]];
cell.detailTextLabel.text=[NSString stringWithFormat:@"%@",[[[[resultArray objectAtIndex:indexPath.section]valueForKey:@"heartrate"] valueForKey:@"date"] objectAtIndex:indexPath.row]];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"date=%@ and heartRate= %@",[[[[resultArray objectAtIndex:indexPath.section]valueForKey:@"heartrate"] valueForKey:@"date"] objectAtIndex:indexPath.row],[[[[resultArray objectAtIndex:indexPath.section]valueForKey:@"heartrate"] valueForKey:@"heartrate"] objectAtIndex:indexPath.row]);
}
を追加します出力画面 -
は、あなたがしようとしているものをいくつかのコードを投稿しますか? –
が、これは... –