【问题标题】:Data from Plist is Incorrectly Ordered in UITableView来自 Plist 的数据在 UITableView 中的排序不正确
【发布时间】:2012-08-09 22:00:05
【问题描述】:

我有存储在 plist 中的数据,但是当我将其拉到 UITableView 时,由于某种原因它会重新排序。以下是我的表格视图数据源方法:

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

这是plist的顺序:

  • 开胃菜
  • 意大利面
  • 比萨饼
  • 特价

这是编译后UITableView中的结果顺序:

  • 比萨饼
  • 开胃菜
  • 意大利面
  • 特价

任何帮助都会很棒! 提前致谢。

【问题讨论】:

  • 为什么要存储不变的东西?对列表进行硬编码。

标签: iphone ios xcode uitableview plist


【解决方案1】:

如果要在 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];

此代码表示很容易复制为 plist。如下例所示创建它

这种结构可以很方便地用作 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];
    ...
}

【讨论】:

  • 由于这是编码而不是 plist,我无法将 plist 存储在服务器中以进行持续更新。我需要向 Apple 提交更新。还是我不明白?
  • 这个结构可以被plist复制。看答案更新
  • 我明白了,感谢您的澄清。我使用您的结构更改了我的 plist。我现在需要更新数据源方法吗?谢谢
  • 我希望你现在明白了。欢迎来到 SO
【解决方案2】:

看起来lunch_DinnerNSDictionaryNSDictionarys 是无序的,因此无法保证它们的特定输出顺序。

很难确切地看到您从这里拥有一个键/值对获得了什么值,但一种选择是保留一个单独的 NSArray 并使用正确的顺序,并使用它来正确填充内容.

【讨论】:

  • 是的,它是一个 NSDictionary。我尝试将NSDictionary 更改为NSArray 并使用arrayWithContentsOfFile 加载plist,就像我对字典所做的那样,但是现在当我编译时,UITableView 中没有出现任何内容。我是否需要在数据源方法中更改其他内容,或者只是将字典更改为不可能的数组? @亚当
  • plist 的键是以数组开头的。 @亚当
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多