所以我的要求是我有这样的数据集
car - toyota
car
white
car - BMW
car
blue
car - Jaguar
car
brown
Bike - KTM
Bike
quả cam
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;
}
- 您只需要在显示这些数据之前更新 TableView 数据源。例如,您可以创建一个 NSDictionary,其中组名(“car”、“bike”等)作为键,NSArray 作为值,因此键“car”的值将是“toyota”、“BMW”的数组", "Jaguar"对象。
- 然后您可以创建一个 NSArray 以按照您需要的顺序保存所有组名。 groupArray = dataDictionary.allKeys;
- 然后可以显示所有组名和该组下的所有项目:
-(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;
}
Tôi là một lập trình viên xuất sắc, rất giỏi!