如何使用 SectionHeaderTitle 在 TableView 中设置数组?
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.
那么我在 table 中设置数据的 TableView 的方法是什么
({
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.
1) 首先,您必须为包含唯一值的章节标题创建一个数组。您可以通过比较数组内的值来使您的值独一无二。
2) 第二件事你必须创建第二个数组,其中包含你的部分的字典。像第 1 部分有 2 行
3) 然后你必须实现tableview数据源方法。
– tableView:cellForRowAtIndexPath: // required method
– tableView:numberOfRowsInSection: // required method
– numberOfSectionsInTableView:
– tableView:titleForHeaderInSection:
我假设您已经将数据添加到 table 视图中的行。
对剖面图使用以下函数。
func numberOfSectionsInTableView(tableView: UITableView) -> Int{}
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String?{}
这些是在 table 视图中显示部分的默认函数。获取日期键(根据您的数据结构)并将其作为标题添加到部分视图中,并将心率添加到部分中的行。如果您不明白这一点,请告诉我,我会相应地修改我的答案:)
希望对您有所帮助。
您可以使用以下方法区分数组中的两个数据:
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
}
由此您可以区分日期和心率,并使用 table 视图委托和数据源。
尝试如下操作(希望这能解决您的问题)-
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
方法-
-(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 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.
那么我在 table 中设置数据的 TableView 的方法是什么
({
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.
1) 首先,您必须为包含唯一值的章节标题创建一个数组。您可以通过比较数组内的值来使您的值独一无二。
2) 第二件事你必须创建第二个数组,其中包含你的部分的字典。像第 1 部分有 2 行
3) 然后你必须实现tableview数据源方法。
– tableView:cellForRowAtIndexPath: // required method
– tableView:numberOfRowsInSection: // required method
– numberOfSectionsInTableView:
– tableView:titleForHeaderInSection:
我假设您已经将数据添加到 table 视图中的行。
对剖面图使用以下函数。
func numberOfSectionsInTableView(tableView: UITableView) -> Int{}
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String?{}
这些是在 table 视图中显示部分的默认函数。获取日期键(根据您的数据结构)并将其作为标题添加到部分视图中,并将心率添加到部分中的行。如果您不明白这一点,请告诉我,我会相应地修改我的答案:)
希望对您有所帮助。
您可以使用以下方法区分数组中的两个数据:
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
}
由此您可以区分日期和心率,并使用 table 视图委托和数据源。
尝试如下操作(希望这能解决您的问题)-
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
方法-
-(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]);
}
这是输出画面-