2

Can anyone help me with getting data from this plist? I'm having trouble accessing the values of the three objects in the plist.

i can see all the list of countries in my tableView, but i can't see the prices when i tap on a cell . any help please thanks

MY PLIST

plist snapshot

<plist version="1.0">
<dict>
    <key>Afghanistan 3</key>
    <array>
        <string>RC $1.65</string>
        <string>CC $2.36</string>
        <string>EC 0</string>
    </array>
    <key>Albania 1</key>
    <array>
        <string>RC FREE</string>
        <string>CC $1.01</string>
    </array>
    <key>Algeria 2</key>
    <array>
        <string>RC $0.27</string>
        <string>CC $0.85</string>
    </array>
    <key>Andorra 2</key>
    <array>
        <string>RC FREE</string>
        <string>CC $0.93</string>

also my code that i have implemented in xcode 4.5 . cc is the calling rate that is in item 0 in the plist rc is the receiving rate that is in item 1 in the plist
ec is the extra rate that is in item 2 in the plist

how can i see the cc ,rc, & ec each in a label when i click the cell in the next view controller ? MY CODE

NSString *ratesFile = [[NSBundle mainBundle] pathForResource:@"rates" ofType:@"plist"];
    rates = [[NSDictionary alloc]initWithContentsOfFile:ratesFile];
    NSArray * dictionaryKeys = [rates allKeys];
    name = [dictionaryKeys sortedArrayUsingSelector:@selector(compare:)];
    cc = [rates objectForKey:@"Item 0"];
    rc = [rates objectForKey:@"Item 1"];
    ec = [rates objectForKey:@"Item 2"];

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [rates count];
}

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }

    NSString *countryName = [name objectAtIndex:indexPath.row];
    cell.textLabel.text = countryName;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   NSString *ccRate = [cc objectAtIndex:indexPath.row];


    if (!self.detailViewController) {
        self.detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
    }

    self.detailViewController.detailItem = ccRate;

    [self.navigationController pushViewController:self.detailViewController animated:YES];
}

thank in advance

4

2 回答 2

1

您对 cc、rc 和 ec 的访问权限不正确。在您的didSelectRowAtIndexPath:方法中,您需要获取与 indexPath 关联的键的数组。然后从那里你得到数组值。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *countryName = [name objectAtIndex:indexPath.row];
    NSArray *countryInfo = [rates objectForKey:countryName];

    // the countryInfo array has the RC, CC, and EC values.
}

请注意,“rates”没有具有“Item 0”、“Item 1”等键的对象。rates 在一个数组中。数组没有键控。

于 2012-10-14T21:37:28.347 回答
0

在 cellForRowAtIndexPath 中执行此操作 - 数组是键(名称)的值

NSString *rc = [[rates objectForKey:name] objectAtIndex:0];
NSString *cc = [[rates objectForKey:name] objectAtIndex:1];
于 2012-10-14T21:34:05.780 回答