1

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

Sanandiya Vipul
  • 764
  • 4
  • 16

4 Answers4

1

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

Rohit Khandelwal
  • 1,778
  • 15
  • 23
0

1) First you have to create one array for your section titles which contains unique value. You can do unique your values by comparing value with each other inside array.

2) Second thing you have to create second array which contains dictionaries for your sections. Like section 1 has 2 rows

3) Then you have to implement tableview datasource methods.

– tableView:cellForRowAtIndexPath: // required method

– tableView:numberOfRowsInSection: // required method

– numberOfSectionsInTableView:

– tableView:titleForHeaderInSection:
Bhadresh Mulsaniya
  • 2,610
  • 1
  • 12
  • 25
0

I am assuming you have already added data to row in table view.

Use the following functions for section view.

func numberOfSectionsInTableView(tableView: UITableView) -> Int{}

func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String?{}

These are by default functions for showing section in table view. Fetch the Date key(according to your data structure) and add it as title in section view and the heart rate to the row in section. If you don't understand this please let me know I'll modify my answer accordingly :)

Hope this will help you.

iOS_MIB
  • 1,885
  • 13
  • 24
0

You can differentiate the both data i array by using this:

for(int i=0; i<[array count]; i++){
 NSDictionary *dic=array[i];
[dateArray addObject:[dic valueForKey:@"date"]]; //date array is mutable array
[heartRateArray addObject:[dic valueForKey:@"heartrate"]]; //mutable array
}

by this you can differentiate both date and heart rate and use the table view delegate and datasource.

Saurabh Jain
  • 1,688
  • 14
  • 28