需要帮助阅读目标c中的plist文件

med*_*eda 1 objective-c plist uitableview

我一直在尝试从plist文件读取数据。这就是它的结构

|term|detail(string)|
Run Code Online (Sandbox Code Playgroud)

我的财产:

@property (nonatomic, strong) NSDictionary *terms;
@property (nonatomic, strong) NSArray *termKeys;//this is just a array to keep track
@property (nonatomic, strong) NSString *detail;
Run Code Online (Sandbox Code Playgroud)

这是我访问cellForRowAtIndexPath中的详细信息的方式

   - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [[UITableViewCell alloc]
                             initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];

    NSString *currentTermsName = [termKeys objectAtIndex :[indexPath row]];
                                  [[cell textLabel] setText:currentTermsName];
    cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;


    detail = [terms objectForKey:[termKeys objectAtIndex:indexPath.row]];

    NSLog(@" bombs %@",terms[@"Bomb Threats"]);
    return cell;

}
Run Code Online (Sandbox Code Playgroud)

鉴于didload我有

 - (void)viewDidLoad
    {
        [super viewDidLoad];


        NSString *myfile = [[NSBundle mainBundle]
                            pathForResource:@"terms" ofType:@"plist"];
        terms = [[NSDictionary alloc] initWithContentsOfFile:myfile];
        termKeys = [terms allKeys];
    }
Run Code Online (Sandbox Code Playgroud)

它访问值,但为每个对象存储相同的值,可以说我在plist中有5条不同的记录,如果我打印详细信息,它将显示5条相同的记录。

Once detail is set then I pass it to detialView
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    if([segue.identifier isEqualToString:@"detailsegue"]){
        TermsDetailViewController *controller = (TermsDetailViewController *)segue.destinationViewController;
        controller.detailTerm = detail;
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的字典:http : //pastebin.com/bxAjJzHp

Anu*_*das 5

您无需保留属性的详细信息,因为在滚动tableView并将代码写入cellForRowAtIndexPath:中时,属性将不断变化。

尝试实现以下代码。

- (void)viewDidLoad{ 

      //Path of plist from bundle
      NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"plistFil" ofType:@"plist"];

     //Since your plist's root is a dictionary else you should form NSArray from contents of plist
      self.terms = [NSDictionary dictionaryWithContentsOfFile:plistPath];

      self.termKeys = [self.terms allKeys];

      [self.tableView reloadData];

     //As you have a key for "Bomb Threats" in your plist,try logging if it successfully initialized with values of plist
      NSLog("%@",terms[@"Bomb Threats"]);
}



 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsForSection:(NSInteger)section{
       return [self.termkeys count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

     //Initialize cell

     NSString *key   = self.termKeys[indexPath.row];
     NSString *value = self.terms[key];

     cell.textLabel.text = key;
     cell.detailTextLabel.text = value;

     return cell;
}
Run Code Online (Sandbox Code Playgroud)

编辑:

//如果segues直接来自单元格,请尝试使用它

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if([segue.identifier isEqualToString:@"detailsegue"])
    {
        TermsDetailViewController *controller = (TermsDetailViewController *)segue.destinationViewController;
        NSIndexPath *indexPath = [ self.tableView indexPathForCell:sender];
        NSString *key   = self.termKeys[indexPath.row];
        self.detail = self.terms[key];
        controller.detailTerm = self.detail;
    }
}
Run Code Online (Sandbox Code Playgroud)