2014-04-01 95 views
0

我很困惑,爲什麼我的PList數據未顯示在表格視圖單元格中。應用程序運行,但單元格是空白的。請你能幫助我知道我哪裏出錯了。謝謝。未在UITableView中顯示Plist數據

片段的plist的(healthlist.plist)

<plist version="1.0"> 
<dict> 
<key>Health</key> 
<array> 
    <dict> 
     <key>title</key> 
     <string>Allergies</string> 
     <key>info</key> 
     <string>Allergies are caused by an overactive immune system. The immune system is designed to protect however when it mistakes non-harmful environmental substances (allergens) as threats then an allergic reation will occur</string> 
     <key>symptoms</key> 
     <string>Itching, licking, chewing the skin or scratching with their feet. Common areas are face, ears, belly, feet and armpit region.</string> 
     <key>common</key> 
     <string>Environmental allergens include dust mites, fleas, mold and pollens. Food allergies or food intolerance to certain ingredients such as, beef, soy, wheat, fish, rice and chicken.</string> 
     <key>cure</key> 
     <string>Treatment options for allergies: corticosteroids, antihistamines, allergy vaccine, shampoos and cyclosporine. Always seek a Vets advice.  </string> 
      <key>image</key> 
      <string>allergy.jpg</string> 

HealthTableViewController.h片段

@interface HeathTableViewController : UITableViewController <UITableViewDelegate,  NSFetchedResultsControllerDelegate> 

@property (nonatomic, strong) NSArray *Health; 
@property (nonatomic, strong) NSArray *title; 
@property (nonatomic, strong) NSArray *info; 
@property (nonatomic, strong) NSArray *symptoms; 
@property (nonatomic, strong) NSArray *common; 
@property (nonatomic, strong) NSArray *cure; 
@property (strong, nonatomic) NSFetchedResultsController *fetchedResultsController; 
@property (strong, nonatomic) NSManagedObjectContext *managedObjectContext; 

HealthViewController.m片斷

- (void)viewDidLoad 
{ 


    [email protected]"Health"; 
    NSString *HealthFile = [[NSBundle mainBundle] pathForResource:@"healthlist" ofType:@"plist"]; 
    NSDictionary *HealthDict = [[NSDictionary alloc] 
          initWithContentsOfFile:HealthFile]; 
    Health = [HealthDict objectForKey:@"health"]; 


[super viewDidLoad]; 

} 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 

    // Return the number of sections. 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
    { 
// Return the number of rows in the section. 
return [Health count]; 
} 

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

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; 

NSDictionary *health = [Health objectAtIndex:indexPath.row]; 
NSString *nameOfTitle = [health objectForKey:@"title"]; 

cell.textLabel.text = nameOfTitle; 

return cell; 

} 
+0

提示:使用一組自定義對象而不是許多通用對象數組。 – trojanfoe

+0

在委託方法中加入斷點並查看它們是否被調用。從那裏打印出來的數據通過NSLog或調試器控制檯和功能 –

回答

0

我發現一個小問題有一個大問題。

你的plist包含一個單個鍵/值對的字典。關鍵是「健康」,但你試圖用「健康」鍵讀取它。字典鍵是大小寫敏感的(就像iOS中的大多數情況一樣)。改正這一點。

你有兩個實例變量和以大寫字母開頭的局部變量。不要這樣做。可可有一個強大的命名約定,其中只有類名和文件名應以大寫字母開頭。方法名稱和變量/屬性名稱應以小寫字母開頭。

兩者均使用「camelCase」,如果您將多個單詞組合在一起,則第一個單詞之後的每個單詞都會大寫。

這不僅僅是一個風格問題。屬性獲得者/設置者使用一種命名約定,該約定假定屬性的名稱以小寫字母開頭。

您應該學習如何使用調試器並設置斷點。否則,至少應該學習如何在程序運行時使用NSLog命令記錄變量的內容。然後,您可以將您的代碼無法按預期執行的操作置零。 NSLog很簡單,但不得不不斷添加新的日誌語句重新編譯和再次測試。

調試器讓您單擊程序行左邊的空白處設置斷點,當程序點擊該斷點時停止,您可以檢查變量甚至更改它們。然後,您可以繼續或單步執行代碼。這是一個很有用的工具,可以幫助您找出代碼中的問題,並且非常值得投入時間來弄清楚。

+0

它工作後,我把密鑰更改爲大寫「健康」。謝謝大家爲您的時間和精力。不勝感激。 – user3320544

+0

很高興幫助。請記住,一旦你有足夠的聲望,就回來投票我的答案。 –