PDA

View Full Version : Table View Won't Load Data




nashyo
Mar 4, 2012, 05:02 AM
I have a project that loads an array into a table view, no problem at all. I have created a new project, that has the exact same code, but a different array, and the table view loads blank. It's driving me nuts.

The array loads a plist as follows

where 'currentContent is a strong pointer to an NSArray private property of the parent view.
- (void) viewDidLoad {
NSString *path = [[NSBundle mainBundle] pathForResource:@"CustomerContent" ofType:@"plist"];
NSArray *myArray = [[NSArray alloc] initWithContentsOfFile:path];
currentContent = myArray;
}

the plist looks like this
http://i40.tinypic.com/34ni8lt.png

table view data source
- (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 [currentContent count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

cell.textLabel.text = [[currentContent objectAtIndex:indexPath.row] objectForKey:@"customerFriendlyFileName"];
cell.detailTextLabel.text = [[currentContent objectAtIndex:indexPath.row] objectForKey:@"publicationDate"];
return cell;
}

the data source is connected in storyboard. the cell reuse identified in the storyboard is named as it is in the code. the above code has been entered in a subclass of uitableviewcontroller.

the storyboard is simply a tableview embedded in a navigation controller. the navigation controller is the initial view controller.

I have NO IDEA why this isn't working. Please help.



nashyo
Mar 4, 2012, 06:54 AM
It is defiantly something to do with my plist. The table view will populate with an NSArray object contaning three strings @"One", @"Two", @"Three", but not the plist.

Why isn't the plist being recognised?

nashyo
Mar 4, 2012, 07:15 AM
The table view is now loading from my plist, but I had to create the plist in code, by writing it into the doc directory of the application sandbox THEN copy and paste it from the doc directory in iPhone simulator to application bundle.

By going to Xcode ->new file -> plist and manually entering the data there, the plist wasn't loading. What's that about?

PhoneyDeveloper
Mar 4, 2012, 10:53 AM
Probably the difference is whether the root of the plist is an array or a dictionary.

nashyo
Mar 4, 2012, 12:23 PM
Probably the difference is whether the root of the plist is an array or a dictionary.

Basic question...how do I change that?

PhoneyDeveloper
Mar 4, 2012, 01:38 PM
If you create the plist in code then it's entirely up to your code. If you create the plist in Xcode it does it in a standard way. Once the plist is created and you're editing it in the plist editor or in Xcode you can change it to whatever you like. The main thing of course is to be able to tell what the root of the plist is in the editor and then make sure it's what you want it to be.

In the screenshot you show it looks like Item 0 is a dictionary. I assume that Item 0 is in an array.

This might be more clear to you if you open the plist file in a text editor where you can see the xml.

TheWatchfulOne
Mar 6, 2012, 06:11 PM
Basic question...how do I change that?

Look in the plist editor in Xcode again. Look under Type on the line for Item 0. If you click where it says Dictionary, you will get a drop down list that lets you change it to an Array.

But, I'm not sure you want to do that because you already have keys and values that are related; and in your cellForRowAtIndexPath method your are retrieving values based on those keys.

If I were you, I would change my viewDidLoad method so that you load the contents of your plist into a NSDictionary instead of a NSArray.

Hope it works out.:cool:

nashyo
Mar 7, 2012, 02:36 AM
If I were you, I would change my viewDidLoad method so that you load the contents of your plist into a NSDictionary instead of a NSArray.

Hope it works out.:cool:

Thanks for the reply. However, I need an array to run the table.

Anyway, I generated a plist list that looks identical and now it works (check my response above).