2013-03-04 121 views
0

我一直在尝试从plist文件中读取数据。这是它的结构需要帮助阅读目标文件中的plist文件c

|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]; 
    } 

它acc esses的值,但它存储每个对象相同的一个 可以说我有5个不同的记录plist,如果我打印的细节它显示相同的记录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

+0

你是什么意思“,它存储相同的一个为每个对象“? – nielsbot 2013-03-04 02:04:15

+0

可以说我有5个不同的记录plist,如果我打印'detail'它显示相同的记录5次 – meda 2013-03-04 02:13:28

+0

我想你应该使用这个:'detail = [[terms objectForKey:@“termKeys objectAtIndex:indexPath.row]] objectForKey:@“detail”]' – nielsbot 2013-03-04 03:25:57

回答

4

您不需要为属性保留详细信息,因为当您滚动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; 
} 

编辑:

//尝试使用这个,如果是塞格斯直接从细胞

- (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; 
    } 
} 
+0

'NSLog(“@%@”,terms [ @“Bomb Threats”]);'打印正确的文本但将'value'传递给'detail'给了我同样的问题 – meda 2013-03-04 16:15:03

+0

你可以显示tableView:cellForRowAtIndexPath:方法的实现吗? – Anupdas 2013-03-04 16:22:42

+0

我已经发布了整个cod e – meda 2013-03-04 17:22:01

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]]; 
+0

我没有objectforkey @“term1”如何添加它? – meda 2013-03-04 01:22:56

+0

这只是一个例子,你应该使用你的术语名称。你不打印出你的字典吗? NSLog(@“%@”,terms);然后在这里发布输出... – 2013-03-04 01:29:01

+0

这里是我的字典http://pastebin.com/gK170rxZ – meda 2013-03-04 01:55:48

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; 
}