2013-03-04 154 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; 
}