ios - 如何使用 SectionHeaderTitle 在 TableView 中设置数组?

标签 ios objective-c iphone nsarray

This is my array and i set in TableView with Section Wise.this array is Dynamically so that value is Increase or decrease.

Set Date in Header Section Title

Same date value is included in One Section.

那么 TableView 的方法是什么,我用 Section wise 在表中设置数据。

({
    date = "2016-07-09 06:46:00 +0000";
    heartrate = 89;
},
    {
    date = "2016-07-07 06:46:00 +0000";
    heartrate = 88;
},
    {
    date = "2016-07-06 06:46:00 +0000";
    heartrate = 90;
},
    {
    date = "2016-07-09 06:46:00 +0000";
    heartrate = 102;
},
    {
    date = "2016-07-07 06:46:00 +0000";
    heartrate = 98;
},
    {
    date = "2016-07-07 06:46:00 +0000";
    heartrate = 97;
})

Here I set Image and I want to set like this with section wise row set.

enter image description here

最佳答案

尝试做如下(希望这会解决你的问题)-

1- 采用全局 NSMutableArrayresultArray

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 方法-

-(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]);
}

这是输出屏幕-

enter image description here

关于ios - 如何使用 SectionHeaderTitle 在 TableView 中设置数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38280628/

相关文章:

iphone - 如何从 DetailView 中推送 UITableView?

objective-c - 动画后 UITextView 不响应触摸事件

ios - 有时会返回 Google 距离矩阵 ZERO_RESULTS

iOS Firebase/Firestore 权限被拒绝

ios - UITableView 不显示

ios - User-Agent 在 NSMutableURLRequest 中没有改变

iphone - 使用 NSNumberFormatter 从国际货币字符串中获取十进制值

iphone - iOS,退出后台时重启动画

iphone - 当文本已添加到 UITextFields 时,如何在 UIReturnKeyGo 和 UIReturnKeyNext 之间切换?

c# - Xamarin 表单 - 网页 View