How to generate Table View Sections Dynamically and group similar values in that particular section?

StackOverflow https://stackoverflow.com/questions/23670985

  •  23-07-2023
  •  | 
  •  

سؤال

So mi requirement is i have dataset like this

car - toyota
      car
      white

car - BMW
      car
      blue

car - Jaguar
      car
      brown

Bike - KTM
       Bike
       orange

Bike - Honda
       Bike
       black

like this i have n number of objects.

i need to generate o/p as following.

section names car,bike... inside car section i need toyota, BMW,Jaguar inside Bike section i need KTM,Hond.

how can i achieve this

Note: I need to create sections and datas dynamically based on the data set

If any one know the solution kindly help me thanks in advance

the codes that i have used

-(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. You just need to update the tableview datasource before showing these data. For example, you can create a NSDictionary with group names("car", "bike" etc) as the key, a NSArray as the value, so the value for key "car" will be an array of "toyota", "BMW", "Jaguar" objects.
  2. You can then create a NSArray to hold all the group names in the order you need. groupArray = dataDictionary.allKeys;
  3. Then you can display all group names and all items under this group:
-(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;
}

نصائح أخرى

You need call reloadData in table view when you change your data set. Your dataSourceDelegate look good, but in method numberOfRowsInSection need return valid rows number, rows number need get from your data set. And you can use for data storing nsdictionary + nsarray for fast access to data.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top