我有timeSlotArr
,其中包含给定格式的数据。
timeSlotArr ++++++++ (
{
availablenow = 0;
callbackSlotId = "96a8a387-a2a1-e611-8101-000c29821561";
callbackSlotName = "07:00 AM";
date = "2016-12-21";
dateInUtc = "2016-12-21T01:30:00Z";
day = Wednesday;
},
{
availablenow = 0;
callbackSlotId = "9aa8a387-a2a1-e611-8101-000c29821561";
callbackSlotName = "09:00 AM";
date = "2016-12-21";
dateInUtc = "2016-12-21T03:30:00Z";
day = Wednesday;
},
{
availablenow = 0;
callbackSlotId = "9ca8a387-a2a1-e611-8101-000c29821561";
callbackSlotName = "10:00 AM";
date = "2016-12-21";
dateInUtc = "2016-12-21T04:30:00Z";
day = Wednesday;
}
)
我希望在UITableView
多个部分及其中的多行显示我的数据。
根据“day”键(sections
是数组)确定部分没有决定,elementsInSection
是部分内的数据
for (NSDictionary *dict in timeSlotArr) {
NSString *day = [dict objectForKey:@"day"];
if (![sections containsObject:day]) {
[sections addObject:day];
NSMutableArray *arr = [[NSMutableArray alloc] init];
[arr addObject:dict];
[elementsInSection addObject:arr];
} else {
NSMutableArray *arr = [elementsInSection objectAtIndex:[sections indexOfObject:day]];
[arr addObject:dict];
[elementsInSection setObject:arr atIndexedSubscript:[sections indexOfObject:day]];
}
}
NSLog(@"elementsInSection ++++++++ %@",elementsInSection);
elementsInSection Returns 1 instead of 3 here is wrong... 3 means data of `timeSlotArr` with key `day`
NSLog(@"sections ++++++++ %@",sections); //Returns 1 which is right
答案 0 :(得分:2)
根据我的经验,您可以通过过滤以下数据来实现表格视图中的上述部分和行结构:
第1步:您需要的部分是Day
。
所以我们将从你的阵列中获得所有独特的日子。
NSArray *uniqueDays = [arrayData valueForKeyPath:@"@distinctUnionOfObjects.day"];
独特日结果:
(
Wednesday,
Monday,
Tuesday
)
步骤2:现在我们需要白天数组只包含当天的数据数组:
NSMutableDictionary *dictData1 = [NSMutableDictionary dictionary];
for (NSString *strDay in uniqueDays) {
NSArray *filteredDay = [arrayData filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(day == %@)", strDay]];
[dictData1 setObject:filteredDay forKey:strDay];
}
过滤后的数据结果:
{
Monday = (
{
availablenow = 0;
callbackSlotId = "9ca8a387-a2a1-e611-8101-000c29821561";
callbackSlotName = "10:00 AM";
date = "2016-12-21";
dateInUtc = "2016-12-21T04:30:00Z";
day = Monday;
},
{
availablenow = 0;
callbackSlotId = "9ca8a387-a2a1-e611-8101-000c29821561";
callbackSlotName = "10:00 AM";
date = "2016-12-21";
dateInUtc = "2016-12-21T04:30:00Z";
day = Monday;
}
);
Tuesday = (
{
availablenow = 0;
callbackSlotId = "9ca8a387-a2a1-e611-8101-000c29821561";
callbackSlotName = "10:00 AM";
date = "2016-12-21";
dateInUtc = "2016-12-21T04:30:00Z";
day = Tuesday;
}
);
Wednesday = (
{
availablenow = 0;
callbackSlotId = "96a8a387-a2a1-e611-8101-000c29821561";
callbackSlotName = "07:00 AM";
date = "2016-12-21";
dateInUtc = "2016-12-21T01:30:00Z";
day = Wednesday;
},
{
availablenow = 0;
callbackSlotId = "9aa8a387-a2a1-e611-8101-000c29821561";
callbackSlotName = "09:00 AM";
date = "2016-12-21";
dateInUtc = "2016-12-21T03:30:00Z";
day = Wednesday;
},
{
availablenow = 0;
callbackSlotId = "9ca8a387-a2a1-e611-8101-000c29821561";
callbackSlotName = "10:00 AM";
date = "2016-12-21";
dateInUtc = "2016-12-21T04:30:00Z";
day = Wednesday;
}
);
}
现在,您可以使用唯一天数作为section
array
。
并dictData
字典作为行数组,并根据部分获取数据。
编辑: 现在在表格视图方法中使用上面的过滤器数组
#pragma mark --- UITableView DataSource methods
//Section
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return uniqueDays.count;
}
//Row
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [[dictData1 objectForKey:uniqueDays[section]] count];
}
//Cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *aCell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
NSArray *arrDayData = [dictData1 objectForKey:uniqueDays [indexPath.section]];
NSDictionary *dictDetail = arrDayData[indexPath.row];
aCell.textLabel.text = dictDetail[@"callbackSlotId"];
return aCell;
}
如果您需要其他任何内容,请告诉我。