Question

I am confused why my PList data is not showing in the table view cells. The App runs however the cells are blank. Please can you help me to know where I am going wrong. Thankyou.

Snippet of 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 Snippet

    @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 snippet

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

}
Was it helpful?

Solution

I see one big problem a few smaller ones.

Your plist contains a dictionary with a single key/value pair. The key is "Health", but you're trying to read it with a key of "health". Dictionary keys are case-sensitve (as are most things in iOS). Correct that.

You have both instance variables and local variables that begin with upper case letters. Don't do that. Cocoa has a strong naming convention where only Class names and filenames should begin with upper-case letters. Method names and variable/property names should start with a lower case.

Both use "camelCase", where if you combine multiple words, each word after the first is capitalized.

This is more than just a style issue. Property getters/setters use a naming convention that assumes that the name of the property starts with lower case.

You should learn how to use the debugger and set breakpoints. Failing that, you should at least learn how to use the NSLog command to log the contents of variables as your program runs. Then you can zero in on where your code is not acting as you expect it to. NSLog is easy, but it is time-consuming to have to keep adding new log statements re-compiling, and testing again.

The debugger lets you click in the margin to the left of a line of your program to set a breakpoint, and when the program hits that breakpoint it stops and you can examine variables and even change them. You can then continue or single-step through your code. This is a very powerful tool for figuring out problems with your code, and well worth investing the time to figure out.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top