简体   繁体   English

来自Plist的数据在UITableView中的顺序不正确

[英]Data from Plist is Incorrectly Ordered in UITableView

I have data that is stored in a plist but when I pull it to my UITableView it gets reordered for some reason. 我有存储在plist中的数据,但是当我将其拉到UITableView时,由于某种原因它会重新排序。 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: 这是plist的顺序:

  • Appetizers 开胃菜
  • Soups 汤类
  • Pastas 面食
  • Pizzas 披萨
  • Specials 特价商品

And this is the resulting order in the UITableView after being compiled: 这是编译后的UITableView中的结果顺序:

  • Pizzas 披萨
  • Soups 汤类
  • Appetizers 开胃菜
  • Pastas 面食
  • Specials 特价商品

Any help would be great! 任何帮助将是巨大的! Thanks in advance. 提前致谢。

If you want to store table in plist it is better to consider the following structure: 如果要将表存储在plist中,最好考虑以下结构:

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. 此代码表示形式易于复制为plist。 Create it as in example below 如下所示创建它

在此处输入图片说明

This structure can be easily used as UITableView datasource 此结构可以轻松用作UITableView数据源

- (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];
    ...
}

It looks like lunch_Dinner is a NSDictionary . 看起来lunch_DinnerNSDictionary NSDictionary s are unordered, so you are not guaranteed a specific output order from them. NSDictionary是无序的,因此不能保证它们有特定的输出顺序。

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. 很难确切地知道从这里拥有键/值对所获得的价值,但是一个选择是保持具有正确顺序的单独NSArray ,并使用它正确地填充事物。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM