Verve IOS Verve IOS - 23 days ago 7
iOS Question

How to Array Set in TableView With SectionHeaderTitle?


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.


So What is the Method of TableView that i set data in table with 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

Answer

Try to do as follow(Hope this will solve your problem)-

1- Take a global NSMutableArray i.e. resultArray;

2- Add these lines of code where you make your array (also work with dynamic array size)-

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);

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

this is the output screen-

enter image description here