gpt4 book ai didi

ios - 如何动态生成 TableView 部分并在该特定部分中对相似值进行分组?

转载 作者:行者123 更新时间:2023-11-28 18:33:22 24 4
gpt4 key购买 nike

所以我的要求是我有这样的数据集

car - toyota
car
white

car - BMW
car
blue

car - Jaguar
car
brown

Bike - KTM
Bike
orange

Bike - Honda
Bike
black

像这样我有n个对象。

我需要如下生成 o/p。

部分名称汽车,自行车...车内部分我需要丰田,宝马,捷豹在自行车部分,我需要 KTM,Hond。

我怎样才能做到这一点

注意:我需要根据数据集动态创建sections和datas

如果有人知道解决方案,请提前帮助我,谢谢

我用过的代码

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return sectionData.count;
//section data has only sections e.g. car,bike.
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 5;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [sectionData objectAtIndex:section];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}

NSManagedObject *carobj = [StoredDataForDisplay objectAtIndex:indexPath.row];

if ([[carobj valueForKey:@"type"] isEqualToString:[sectionData objectAtIndex:indexPath.section]] ) {
cell.textLabel.text = [carobj valueForKey:@"name"];

}
return cell;
}

最佳答案

  1. 您只需要在显示这些数据之前更新 TableView 数据源。例如,您可以创建一个 NSDictionary,其中组名(“car”、“bike”等)作为键,NSArray 作为值,因此键“car”的值将是“toyota”、“BMW”的数组", "Jaguar"对象。
  2. 然后您可以创建一个 NSArray 以按照您需要的顺序保存所有组名。 groupArray = dataDictionary.allKeys;
  3. 然后可以显示所有组名和该组下的所有项目:
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return groupArray.count;
//section data has only sections e.g. car,bike.
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[dataDictionary objectForKey:groupArray[section]] count];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [groupArray objectAtIndex:section];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}

NSManagedObject *carobj = [[dataDictionary objectForKey:groupArray[indexPath.section]] objectAtIndex:indexPath.row];
cell.textLabel.text = [carobj valueForKey:@"name"];

return cell;
}

关于ios - 如何动态生成 TableView 部分并在该特定部分中对相似值进行分组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23670985/

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