0

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 での結果の順序です。

  • ピザ
  • スープ
  • 前菜
  • パスタ
  • 特別

どんな助けでも素晴らしいでしょう!前もって感謝します。

4

2 に答える 2

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];
    ...
}
于 2012-08-09T22:22:56.267 に答える
0

のようlunch_DinnerですNSDictionaryNSDictionaryは順序付けされていないため、それらからの特定の出力順序は保証されません。

ここでキーと値のペアを持つことから得られる値を正確に確認するのは少し難しいですが、1 つのオプションはNSArray、正しい順序で別のものを保持し、それを使用して正しく設定することです。

于 2012-08-09T22:08:07.103 に答える