Data from Plist is Incorrectly Ordered in UITableView

大憨熊 提交于 2019-12-11 20:13:23

问题


I have data that is stored in a plist but when I pull it to my UITableView it gets reordered for some reason. Here are my table view data source methods:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [self.lunch_Dinner count];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [[self.lunch_Dinner allKeys] objectAtIndex:section];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSString *typeOfEntree = [self tableView:tableView titleForHeaderInSection:section];
    return [[self.lunch_Dinner valueForKey:typeOfEntree] count];
}

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

    // Configure the cell...
    NSString *typeOfEntree = [self tableView:tableView titleForHeaderInSection:indexPath.section];
    NSString *entree = [[self.lunch_Dinner valueForKey:typeOfEntree] objectAtIndex:indexPath.row];

    cell.textLabel.text = entree;

    return cell;
}

Here is the order of the plist:

  • Appetizers
  • Soups
  • Pastas
  • Pizzas
  • Specials

And this is the resulting order in the UITableView after being compiled:

  • Pizzas
  • Soups
  • Appetizers
  • Pastas
  • Specials

Any help would be great! Thanks in advance.


回答1:


If you want to store table in plist it is better to consider the following structure:

NSArray *sections = [NSArray arrayWithObjects:
    [NSDictionary dictionaryWithObject:
        [NSArray arrayWithObjects:row_1, row_2, ... , nil] 
            forKey:@"section name 1"],
    [NSDictionary dictionaryWithObject:
        [NSArray arrayWithObjects:row_1, row_2, ... , nil] 
            forKey:@"section name 2"],
    ...
    [NSDictionary dictionaryWithObject:
        [NSArray arrayWithObjects:row_1, row_2, ... , nil] 
            forKey:@"section name N"], 
    nil];

This code representation is easy to reproduce as plist. Create it as in example below

This structure can be easily used as UITableView datasource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [self.datasource count];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    NSDictionary *dict = [self.datasource objectAtIndex:section];
    return [dict.allKeys lastObject];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSDictionary *dict = [self.datasource objectAtIndex:section];
    return [dict.allValues.lastObject count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dict = [self.datasource objectAtIndex:indexPath.section];
    id cellPresenter = [dict.allValues.lastObject objectAtIndex:indexPath.row];
    ...
}



回答2:


It looks like lunch_Dinner is a NSDictionary. NSDictionarys are unordered, so you are not guaranteed a specific output order from them.

It is a little hard to see exactly what value you're getting from having a key/value pair here, but one option would be to keep a separate NSArray with the correct ordering, and use that to correctly populate things.



来源:https://stackoverflow.com/questions/11892598/data-from-plist-is-incorrectly-ordered-in-uitableview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!