Plist到UITableView

时间:2012-12-11 10:07:52

标签: iphone ios ipad uitableview plist

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

enter first 我需要在UITableView中填充上面的图像,使得数组a, b, c应该出现在section字段中。它正好在下面。但我无法填充数据 请检查我错在哪里。

second     

@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>

3 个答案:

答案 0 :(得分: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];

答案 1 :(得分:0)

我认为你没有在你的数据层次结构中钻得足够远。在numberOfRowsInSection:中,您将获得根级别字典中键的值:看起来这总是NSArray,只有一个项目:字典。你可能想要像

这样的东西
...
NSDictionary *dataInSection = [[self.myData objectForKey:key] objectAtIndex:0];
return [dataInSection count]; 
...

答案 2 :(得分: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可能会帮助您提供此信息..