Plist数据未在UITableView中显示

时间:2014-04-01 20:10:50

标签: ios objective-c xcode uitableview plist

我很困惑为什么我的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
{


   self.navigationItem.title=@"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;

}

1 个答案:

答案 0 :(得分:0)

我看到一个小问题的一个大问题。

您的plist包含一个包含单个键/值对的字典。关键是“健康”,但你试图用“健康”键来阅读它。字典键是大小写敏感的(iOS中的大部分内容都是如此)。纠正这个。

您有实例变量和以大写字母开头的局部变量。不要那样做。 Cocoa有一个强大的命名约定,其中只有类名和文件名应以大写字母开头。方法名称和变量/属性名称应以小写字母开头。

两者都使用“camelCase”,如果你组合多个单词,则第一个单词后面的每个单词都大写。

这不仅仅是一个风格问题。属性getter / setter使用命名约定,该约定假定属性的名称以小写开头。

您应该学习如何使用调试器并设置断点。如果做不到这一点,您至少应该学习如何使用NSLog命令在程序运行时记录变量的内容。然后,您可以将代码未按预期执行的位置归零。 NSLog很简单,但是必须继续添加新的日志语句重新编译并再次测试是非常耗时的。

调试器允许您单击程序行左侧的边距来设置断点,当程序点击该断点时它会停止,您可以检查变量甚至更改它们。然后,您可以继续或单步执行代码。这是一个非常强大的工具,用于解决代码问题,非常值得花时间去弄清楚。