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

时间:2013-03-04 00:52:13

标签: objective-c uitableview plist

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

|term|detail(string)|

我的属性:

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

这是我访问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;

}

并且在视图中我有

 - (void)viewDidLoad
    {
        [super viewDidLoad];


        NSString *myfile = [[NSBundle mainBundle]
                            pathForResource:@"terms" ofType:@"plist"];
        terms = [[NSDictionary alloc] initWithContentsOfFile:myfile];
        termKeys = [terms allKeys];
    }

它访问值,但它为每个对象存储相同的值 假设我在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;
    }
}

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

3 个答案:

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

编辑:

//如果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;
    }
}

答案 1 :(得分:0)

我就是这样做的,

 NSString *plistFile = [[NSBundle mainBundle] pathForResource:@"myPlist" ofType:@"plist"];
 NSDictionary *terms = [NSDictionary dictionaryWithContentsOfFile:plistFile];
 NSLog(@"%@", [terms objectForKey:@"term1"]);
在你的代码中

如果

termKeys = [terms allKeys];

那么这应该返回正确的值,

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

答案 2 :(得分:0)

试试这个

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

static NSString *CellIdentifier = @"Cell";

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

NSString *strPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
        strPath = [strPath stringByAppendingPathComponent:@"fileName.plist"];
NSMutableDictionary  *terms = [NSMutableDictionary dictionaryWithContentsOfFile:strPath];
NSArray *arrTemp =[terms allKeys];
arrTemp = [arrTemp sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
NSString *detail = [terms objectForKey:[termKeys objectAtIndex:indexPath.row]];
cell.textLabel.text = detail;

return cell;
}