1

UITableViewファイルからの情報をに入力しようとしていplistます。どこが悪いのかわかりません。私の要件と上記のコードの画像をチェックして、私が間違っている場所を見つけてください

最初に入力してくださいUITableView配列a, b, cがセクションフィールドに表示されるように、 上の画像にデータを入力する必要があります。そのすぐ下に来る。しかし、データを入力できません。どこが間違っているかを確認してください。。

2番目

@synthesize mySections  //array
@synthesize myData    //dictionary    

- (void)viewDidLoad
{
    [super viewDidLoad];

    //LOADING THE PLIST FILE

    //Create a string representing the file path
    NSString *plistPath = [bundle pathForResource:@"yourFilename" ofType:@"plist"];

    //Load the file in a dictionnary
    NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:plistPath];

    self.myData = dict;

    //SORTING THE DICTIONARY    
    NSArray *dicoArray = [[self.myData allKeys] sortedArrayUsingComparator:^(id firstObject, id secondObject) {
          return [((NSString *)firstObject) compare:((NSString *)secondObject) options: NSCaseInsensitiveSearch];
    }];

    self.mySections = dicoArray;
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    //Return the number of sections.
    return [self.mySections count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    NSString *key = [self.mySections objectAtIndex:section];
    NSArray *dataInSection = [self.myData objectForKey:key];
    return [dataInSection count];
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    NSString *key = [self.mySections objectAtIndex:section];
    return [NSString stringWithFormat:@"%@", key];
}

-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    return self.mySections;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
    }

    // Configure the cell...


    NSUInteger section = [indexPath section];
    NSUInteger row = [indexPath row];

    NSString *key = [self.mySections objectAtIndex:section];

    NSDictionary *Dic=self.myData;   
    NSArray *dataForSection = [self.myData objectForKey:Dic.allKeys];



    cell.textLabel.text = [dataForSection objectAtIndex:row];

    return cell;    

}

plistのコードは次のとおりです:-

<plist version="1.0">
<dict>
<key>a</key>
<array>
    <dict>
        <key>Beta_Carot_(µg)</key>
        <string>0.06</string>
        <key>Cholestrl_(mg)</key>
        <string>215</string>
        <key>fdgdfg</key>
        <string>dfgdfg</string>
    </dict>
</array>
<key>b</key>
<array>
    <dict>
        <key>Alpha_Carot_(µg)</key>
        <string>0</string>
        <key>gdfg</key>
        <string>fdgdfg</string>
    </dict>
</array>
<key>c</key>
<array>
    <dict>
        <key>Alpha_Carot_(µg)</key>
        <string>0</string>
        <key>Ash_(g)</key>
        <string>0</string>
        <key>Beta_Caro</key>
        <string>193</string>
        <key>Choline_Tot_ (mg)</key>
        <string>22.3</string>
        <key>dgd</key>
        <string>dgdf</string>
    </dict>
</array>
</dict>
</plist>
4

3 に答える 3

1

各セクションの行数を決定するときは、適切なデータ構造の要素数を数える必要があります。だから- tableView: numberOfRowsInSection:あなたは持っている必要があります

NSArray *dataInSection = [[self.myData objectForKey:key] objectAtIndex:0];
return [dataInSection count];

にも問題があり- tableView: cellForRowAtIndexPath:ます。セクションのデータを取得するための正しいキーを作成します*keyが、代わりにDic.allKeysからデータを取得するためのキーとして使用しますself.myData。したがって、を設定すると、配列全体がキーとして送信されます*dataForSection

したがって、これらの行を変更します。

NSString *key = [self.mySections objectAtIndex:section];

NSDictionary *Dic=self.myData;   
NSArray *dataForSection = [self.myData objectForKey:Dic.allKeys];

cell.textLabel.text = [dataForSection objectAtIndex:row];

に:

NSString *key = [self.mySections objectAtIndex:section];
NSDictionary *dataForSection = [[self.myData objectForKey:key] objectAtIndex:0];

cell.textLabel.text = [[dataForSection allKeys] objectAtIndex:row];
于 2012-12-11T10:30:52.803 に答える
0

データ階層を十分に掘り下げていないと思います。ルートレベルのディクショナリでキーの値を取得している場合:これは常に単一のアイテム(ディクショナリ)のみであるnumberOfRowsInSection:ように見えます。NSArrayあなたはおそらく次のようなものが欲しいでしょう

...
NSDictionary *dataInSection = [[self.myData objectForKey:key] objectAtIndex:0];
return [dataInSection count]; 
...
于 2012-12-11T10:29:42.237 に答える
0

私によると、plist plzからのテーブルフェッチデータでいくつかの間違いをしています。これを試してください-(void)viewDidLoad {[super viewDidLoad];

//LOADING THE PLIST FILE

//Create a string representing the file path
NSString *plistPath = [bundle pathForResource:@"yourFilename" ofType:@"plist"];
NSMutableArray *listArray=[NSMutableArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"yourFilename" ofType:@"plist"]];

self.sections = [[NSMutableDictionary alloc] init];

}

次に、必要に応じてデータを設定します。データplistは配列として提供され、dictこの情報が役立つ場合があります。

于 2012-12-11T10:52:28.037 に答える