1

我一直在尝试从 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;

}

并鉴于 didload 我有

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

4

3 回答 3

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;
    }
}
于 2013-03-04T05:06:18.037 回答
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]];
于 2013-03-04T01:07:24.517 回答
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;
}
于 2013-03-04T05:44:32.937 回答