1

我正在尝试使用文件中的UITableView信息填充 a plist。我不知道我哪里错了。请检查我的要求的图像和上面的代码以找出我错的地方

先进入 我需要以UITableView数组a, b, c应该出现在部分字段中的方式填充上面的图像。它就在下面。但我无法填充数据,请检查我错在哪里。.

第二 .

@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 获取数据时犯了一些错误,请试试这个 - (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 回答