gpt4 book ai didi

ios - 具有多行和多节的 UITableview

转载 作者:行者123 更新时间:2023-11-28 21:16:22 25 4
gpt4 key购买 nike

我有 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 中的多个部分和多个行中显示我的数据。

没有section是根据“day”键决定的(sections是数组),elementsInSection是sections里面的数据

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

最佳答案

根据我的经验,您可以通过如下所示过滤数据来在表格 View 中实现上述部分和行结构:

第 1 步:您需要的部分是Day所以我们将从您的数组中获取所有独特的日子。

NSArray *uniqueDays = [arrayData valueForKeyPath:@"@distinctUnionOfObjects.day"];

唯一天数结果:

(
Wednesday,
Monday,
Tuesday
)

第 2 步:现在我们需要只包含当天数据的 day wise 数组:

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

现在您可以使用 Unique Days 作为 section array

dictData字典作为行数组并根据部分获取数据。

编辑: 现在在 TableView 方法中使用上面的过滤器数组

#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;
}

如果您还需要其他任何东西,请告诉我。

关于ios - 具有多行和多节的 UITableview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41236395/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com