Cannot Feed UITableView with .plist - ios

I am a beginner and have a probably quite simple question. I browsed through similar topics and lost a day without finding a solution.
I have a Master and Detail view controllers. I created a .plist as shown below.
Now I would like feed UITableView with these data. And here the problem is. I can't take continents names. All the time I see blank cells or I get some errors. Maybe I should change something in the plist?
Here is my code:
MasterViewController.h:
#interface MasterViewController : UITableViewController
#property (nonatomic, strong) NSDictionary *world;
#property (nonatomic, strong) NSDictionary *africa;
#property (nonatomic, strong) NSDictionary *europe;
#end
MasterViewController.m (the places where I added something):
#synthesize world, africa, europe;
- (void)viewDidLoad
{
self.navigationItem.title = #"World Info";
NSString *worldLibraryFile = [[NSBundle mainBundle] pathForResource:#"World" ofType:#"plist"];
world = [[NSDictionary alloc] initWithContentsOfFile:worldLibraryFile];
africa = [world objectForKey:#"Africa"];
europe = [world objectForKey:#"Europe"];
[super viewDidLoad];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [world count]; //this works fine
}
And here I have a problem:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
NSString *ContinentName = [world objectForKey:indexPath.row]; //I do something wrong here
cell.textLabel.text = ContinentName;
return cell;
}
How can I get my continents shown in the table view? How to get countries and then other info shown after another clicks?
Thank you for any help.
It works now, after these changes:
NSArray *namesOfContintents = [world allKeys];
NSString *ContinentName = [namesOfContintents objectAtIndex:indexPath.row];
cell.textLabel.text = ContinentName;
I also wanted to add some subtitles to the cells. I added:
int numberEurope;
numberEurope = [europe count];
int numberAfrica;
numberAfrica = [africa count];
NSNumber *myNum1 = [NSNumber numberWithInt:numberEurope];
NSNumber *myNum2 = [NSNumber numberWithInt:numberAfrica];
NSArray *myArray = [NSArray arrayWithObjects: myNum1, myNum2, nil];
cell.textLabel.text = ContinentName;
cell.detailTextLabel.text = [[NSString alloc] initWithFormat:#"%# countries", myArray];
return cell;
But the subtitles are the same in every cell: ( 2, 2) countries instead of 2 countries in the first cell and 2 countries in the second one. What am I doing wrong?

NSString *ContinentName = [world objectForKey:indexPath.row];
the objectForKey is expecting a name for the key, you´re giving in an int.
try this:
NSDictionary *ContinentName = [world objectForKey:#"Europe"];
Also, the value returning here is a NSDictionary, not a String.
I recommend converting your continents to a flat array.
NSMutableArray *flatArray = [[NSArray alloc] init];
for(id item in world){
[flatArray addObject:item];
}
This array you can access via the indexPath.row
If you want the names of the continents you can call.
NSArray *namesOfContintents = [world allKeys];

Related

Get only certain Strings from plist iOS

I have a plist and I want ot grab only the items which 'Name' string begins with 'A'. I want then display these in my UITableView, along with the 'NameOrigin' included in the item. I have included a screen shot of my plist. Thanks
My code so far, I am able to display all of the plist items, but I would like to filter the items displayed to ones with string 'Name' being with the letter 'A'?
-(NSArray *)content
{
//if (!_content) {
_content = [[NSArray alloc]initWithContentsOfFile:[[NSBundle mainBundle]pathForResource:#"MyPLIST" ofType:#"plist"]];
// }
return _content;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [TableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.textLabel.text = [[self.content objectAtIndex:indexPath.row] objectForKey:#"Name"];
cell.detailTextLabel.text = [[self.content objectAtIndex:indexPath.row] valueForKey:#"NameOrigin"];
return cell;
}
If you want to filter for only elements where Name begins with "A", try this:
NSArray *filteredArray = [self.content filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"Name beginswith %#", #"A"]];
While we're at it: you should consider refactoring your code a bit. Re-loading the data from file every time you call [self content] is a bad idea, performance wise. Instead try this:
// Create a property called content in your class extension on the top of the .m file of your table view controller
#interface YourTableViewController ()
#property (nonatomic, strong) NSArray *content;
#end
Next, add this code in your viewDidLoad callback:
_content = [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"MyPLIST" ofType:#"plist"]];
and delete the custom content method.

NSDictionary allkeys to UITableViewCell

I am trying to get my NSdictionary values into a UITableViewCell. Here is my dictionary format:
{
date = "3/4/14, 3:33:01 PM Pacific Standard Time";
weight = 244;
}
Here is the code I'm using to populate my uitableview (which is not working).
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"WeightCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
NSArray* allKeys = [weights allKeys];
NSDictionary *obj = [allKeys objectAtIndex: indexPath.row];
cell.textLabel.text = [obj objectForKey: #"date"];
cell.detailTextLabel.text = [obj objectForKey:#"weight"];
return cell;
}
You should try initialising the array for the tableView outside of the tableView itself... something along the lines of
- (void)viewDidLoad
{
[super viewDidLoad];
_allKeys = [[NSMutableArray alloc] initWithArray:[weights allKeys]];
}
Once you have initialised that data you can access it throughout the process. Also to find out how many rows your tableview needs.
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [_allKeys count];
}
Then when you access the array into a dictionary it has retained the row count and can access it properly.
NSDictionary *obj = [_allKeys objectAtIndex: indexPath.row];
cell.textLabel.text = [obj objectForKey: #"date"];
cell.detailTextLabel.text = [obj objectForKey:#"weight"];
From what I can see the dictionary cant access the array at your indexPath.row because you haven't initialised the array anywhere before you use it in your tableView.
Hope that helps, T
Some of the other posters have good suggestions. However, this line:
NSDictionary *obj = [allKeys objectAtIndex: indexPath.row];
Is wrong. allKeys is an array of your dictionary keys, which are presumably strings.
So, you want code like this instead:
NSString *thisKey = allKeys[indexPath.row];
NSDictionary *obj = weights[thisKey];
Note that I am using the new Objective C literal syntax. The expression weights[thisKey] is equivalent to [weights objectForKey: thisKey]
I don't see the definition of the weights object. If you want to keep adding NSDictionary's to an array, you need to use an NSMutableArray, and you'll probably want to do that by setting it as a #property on your class. Let's say you added it like this:
#property (strong, nonatomic) NSMutableArray *weights;
Then in your tableView:cellForRowAtIndexPath: method you'll want to get the NSDictionary corresponding to that line by using self.weights[indexPath.row]. Also don't forget to instantiate weights before using it, otherwise it will return nil and no objects are going to be added to it.
P.S.: the user provided some context here and what he probably needs is Core Data.

Creating a New Array in each iteration of For Loop

After so long, I'm still trying to create my first iOS app, and I'm facing another problem yet again =(
Basically, I have pulled JSON data from the web, and I get nested arrays. I have no problems iterating all objects in the array (including the parent array) and placing them in a UITableView. The problem is, I am trying to do something dynamic for the table. I am able to create the correct number of sections for the table. To make it easy for you to visualise what I want to do, here is the JSON data:
{"filter_group":[{"filter_group_id":"1","name":"Location","filter":[{"filter_id":"2","name":"Central"},{"filter_id":"8","name":"East"},{"filter_id":"1","name":"North"},{"filter_id":"10","name":"Northeast"},{"filter_id":"9","name":"West"}]},{"filter_group_id":"3","name":"Price","filter":[{"filter_id":"7","name":"Free"},{"filter_id":"5","name":"$0 - $50"},{"filter_id":"6","name":"$50 - $100"},{"filter_id":"11","name":"$100 - $150"},{"filter_id":"12","name":"$150 - $200"},{"filter_id":"13","name":"Above $200"}]}]}
When you copy the messy chunk that I give you into any JSON viewer, you will see that for now, I have 2 parent arrays with some children each. So basically, "Location" and "Price" will go into different sections. I have successfully done that, but I don't know how to put the names of their children in the correct section of the UITableView.
So my idea now is that I am iterating the array, and putting the children names into another array (this results in names of both sections going into 1 array). I was thinking of creating a new array in each iteration:
for(int i = 0; i < [myArray count]; i++) {
//throw children of different parents into different array
NSMutableArray* newArray[i] = (blah blah blah)
}
You will probably have seen the problem now: newArray[i]. Basically, I can live with the fact that I am creating newArray0, newArray1 etc (I need my app to be dynamic). How do I do this in iOS programming?
OR
After reading what I want to do above, and you have a different idea on how I am to put the data in the respective sections, please enlighten me.
Thank you very much!!! I greatly appreciate any help offered =D
P.S. My replies may be slow, so please forgive me
In your application you can use array from json object. I've made sample project to illustrate this aproche:
In header file (ViewController.h):
#import <UIKit/UIKit.h>
#interface ViewController : UITableViewController <UITableViewDelegate, UITableViewDataSource>
#property (nonatomic, strong) NSMutableArray *data;
#property (nonatomic, strong) NSMutableArray *sectionTitles;
#end
and in ViewController.m file:
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *jsonString = #"{\"filter_group\":[{\"filter_group_id\":\"1\",\"name\":\"Location\",\"filter\":[{\"filter_id\":\"2\",\"name\":\"Central\"},{\"filter_id\":\"8\",\"name\":\"East\"},{\"filter_id\":\"1\",\"name\":\"North\"},{\"filter_id\":\"10\",\"name\":\"Northeast\"},{\"filter_id\":\"9\",\"name\":\"West\"}]},{\"filter_group_id\":\"3\",\"name\":\"Price\",\"filter\":[{\"filter_id\":\"7\",\"name\":\"Free\"},{\"filter_id\":\"5\",\"name\":\"$0 - $50\"},{\"filter_id\":\"6\",\"name\":\"$50 - $100\"},{\"filter_id\":\"11\",\"name\":\"$100 - $150\"},{\"filter_id\":\"12\",\"name\":\"$150 - $200\"},{\"filter_id\":\"13\",\"name\":\"Above $200\"}]}]}" ;
NSDictionary *result = [NSJSONSerialization JSONObjectWithData:[jsonString dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil];
NSArray *filter_groups = [result objectForKey:#"filter_group"];
self.data = [[NSMutableArray alloc] initWithCapacity:[filter_groups count]];
self.sectionTitles = [[NSMutableArray alloc] initWithCapacity:[filter_groups count]];
for (NSDictionary *filter_group in filter_groups) {
NSArray *filters = [filter_group objectForKey:#"filter"];
[self.data addObject:filters];
[self.sectionTitles addObject:[filter_group objectForKey:#"name"]];
}
NSLog(#"%#", self.data);
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
return self.sectionTitles[section];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return [self.data count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
NSArray *sectionArray = self.data[section];
return [sectionArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"testCell"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"testCell"];
}
NSArray *sectionArray = self.data[indexPath.section];
NSDictionary *filter = sectionArray[indexPath.row];
cell.textLabel.text = [filter objectForKey:#"name"];
return cell;
}
#end
If you have same question to code, feel free to ask. You can grab code from https://gist.github.com/MaciejGad/8452791
create a new temp array in each loop and add that new temp array to a Mutable array, it is similar to new array[i], while retrieving NSArray *array = [mutablearray objectAtIndex:i]`
please try the following code.
NSMutableDictionary *responseDict; // assign value to responseDict from your response
NSMutableArray *mainArray=[[NSMutableArray alloc] initWithArray:[responseDict objectForKey:#"filter_group"]];
for(int i=0;i<[mainArray count];i++)
{
if([[[mainArray objectAtIndex:i] objectForKey:#"name"] isEqualToString:#"Price"])
{
NSArray *pricechildArray=[[mainArray objectAtIndex:i] objectForKey:#"filter"];
}
else if([[[mainArray objectAtIndex:i] objectForKey:#"name"] isEqualToString:#"Location"])
{
NSArray *locationchildArray=[[mainArray objectAtIndex:i] objectForKey:#"filter"];
}
}
hope this will help you.

How to make a tableview divided into sections by letter like the contacts app

I'm trying to replicate the contacts table view in my app. So I have a list of contacts displayed in a table view however I would like the table view to be sectioned into all the letters of the alphabet and the names of the contacts to be placed in the section related to the lister letter of their first name. Like this
How do I get that view? So far this is all I have done.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [displayNames count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"ContactsCell";
/*ContactCell *cell = (ContactCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"ContactCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}*/
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
// cell.name.text = [displayNames
// objectAtIndex:indexPath.row];
UILabel *namer = (UILabel *)[cell viewWithTag:101];
namer.text=[displayNames
objectAtIndex:indexPath.row];
/*
Get the picture urls from the picture array and then
I loop through the array and initialize all the urls with
a NSUrl and place the loaded urls in anouther nsmutable array
*/
urls = [[NSMutableArray alloc] init];
for (id object in pictures) {
//NSDictionary *names = res[#"image"];
NSString *name = object;
NSURL *url=[[NSURL alloc] initWithString:name];
[urls addObject:url];
}
// cell.profile.image= [UIImage imageWithData:[NSData dataWithContentsOfURL: [urls objectAtIndex:indexPath.row]]];
UIImageView *profiler = (UIImageView *)[cell viewWithTag:100];
profiler.image= [UIImage imageWithData:[NSData dataWithContentsOfURL: [urls objectAtIndex:indexPath.row]]];
return cell;
}
Here is an easy solution using the 3rd party TLIndexPathTools data model class TLIndexPathDataModel. It is specifically designed for working with index paths and sections, so you can accomplish what you need with minimal complexity. And here is a full, working demo.
First define a class to represent a contact. This gives you a place to define firstName, lastName, displayName and sectionName:
#interface Contact : NSObject
#property (strong, nonatomic, readonly) NSString *firstName;
#property (strong, nonatomic, readonly) NSString *lastName;
#property (strong, nonatomic, readonly) NSString *displayName;
#property (strong, nonatomic, readonly) NSString *sectionName;
- (instancetype)initWithFirstName:(NSString *)firstName lastName:(NSString *)lastName;
#end
The sectionName property just returns the first character of the firstName. Then if your table view subclasses TLTableViewController, the implementation would look something like this:
#implementation ContactsTableViewController
- (void)viewDidLoad
{
[super viewDidLoad];
NSMutableArray *contacts = [NSMutableArray array];
//get actual list of contacts here...
[contacts addObject:[[Contact alloc] initWithFirstName:#"John" lastName:#"Doe"]];
[contacts addObject:[[Contact alloc] initWithFirstName:#"Sally" lastName:#"Smith"]];
[contacts addObject:[[Contact alloc] initWithFirstName:#"Bob" lastName:#"Marley"]];
[contacts addObject:[[Contact alloc] initWithFirstName:#"Tim" lastName:#"Cook"]];
[contacts addObject:[[Contact alloc] initWithFirstName:#"Jony" lastName:#"Ives"]];
[contacts addObject:[[Contact alloc] initWithFirstName:#"Henry" lastName:#"Ford"]];
//sort by section name
[contacts sortUsingDescriptors:#[[NSSortDescriptor sortDescriptorWithKey:#"sectionName" ascending:YES]]];
//set the data model
self.indexPathController.dataModel = [[TLIndexPathDataModel alloc] initWithItems:contacts sectionNameKeyPath:#"sectionName" identifierKeyPath:nil];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];
//get contact for index path from data model and configure cell
Contact *contact = [self.indexPathController.dataModel itemAtIndexPath:indexPath];
cell.textLabel.text = contact.displayName;
return cell;
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
return self.indexPathController.dataModel.sectionNames;
}
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
return index;
}
#end
The key thing is that TLIndexPathDataModel automatically organizes your data into sections using the sectionNameKeyPath set to #"sectionName". Then in your view controller logic, you can easily access the contact for a given index path by calling:
Contact *contact = [self.indexPathController.dataModel itemAtIndexPath:indexPath];
update
You'd actually want to do a second-level sort on display name:
[contacts sortUsingDescriptors:#[[NSSortDescriptor sortDescriptorWithKey:#"sectionName" ascending:YES], [NSSortDescriptor sortDescriptorWithKey:#"displayName" ascending:YES]]];
update #2
There is a new block-based initializer for TLIndexPathDataModel that makes this a lot easier if you don't want to define a custom data object just to add a sectionNameKeyPath property. For example, one can use the new initializer to organize a list of strings as illustrated in the "Blocks" sample project:
- (void)viewDidLoad
{
[super viewDidLoad];
NSArray *items = [#[
#"Fredricksburg",
#"Jelly Bean",
...
#"Metadata",
#"Fundamental",
#"Cellar Door"] sortedArrayUsingSelector:#selector(caseInsensitiveCompare:)];
//generate section names by taking the first letter of each item
self.indexPathController.dataModel = [[TLIndexPathDataModel alloc] initWithItems:items
sectionNameBlock:^NSString *(id item) {
return [((NSString *)item) substringToIndex:1];
} identifierBlock:nil];
}

plist to tableview when root is array then dictionary

I see example and tutorial after tutorial of how to load tableview from plist when root is dictionary but I have to use a plist that is an array.
plist setup:
root Array
Item 0 Dictionary
name String
Item 1 Dictionary
name String
...
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *path = [[NSBundle mainBundle] pathForResource:#"food" ofType:#"plist"];
NSArray *tableData = [[NSArray alloc]initWithContentsOfFile:path];
NSArray *thumbnails = [[NSArray alloc] init];
for (NSDictionary *dict in tableData){
NSLog(#"%#",dict); // prints all key value pairs in dictionary
thumbnails = [dict objectForKey:#"name"];
}
NSLog(#"outside %#", thumbnails); // this prints last value added to thumbnails array
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"SimpleTableItem";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
NSLog(#"%#", thumbnails); // this loads last value added to array thumbnails
cell.textLabel.text = [thumbnails objectAtIndex:indexPath.row];
return cell;
}
I'm doing this wrong...it doesn't load into my tableview and crashes. I think my for loop is wrong and I think my objectAtIndex is wrong because it crashes at that line. I'm more than happy to share more information. I have datasource and delegate of tableview hooked up to file's owner. I've tested that the tableview works with loading an array directly into it. Please help, I appreciate it.
EDIT:
I placed declaration for the mutable array thumbnails at the top of my .m file as you see below:
#interface SimpleTableViewController ()
#property (nonatomic, strong) NSMutableArray *thumbnails;
#end
These changes still unfortunately leave me with empty thumbnails array in cellForRowAtIndexPath
Same when #property is placed into .h file
EDIT: .m file (latest code)
#import "SimpleTableViewController.h"
#interface SimpleTableViewController ()
#end
#implementation SimpleTableViewController
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *path = [[NSBundle mainBundle] pathForResource:#"food" ofType:#"plist"];
NSArray *tableData = [[NSArray alloc]initWithContentsOfFile:path];
NSMutableArray *thumbnails = [NSMutableArray array];
for (NSDictionary *dict in tableData){
NSLog(#"%#",dict); // prints all key value pairs in dictionary
[thumbnails addObject:dict[#"name"]];
}
NSLog(#"outside %#", thumbnails);
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 50;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"SimpleTableItem";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
NSLog(#"other method%#", self.thumbnails); // this loads last value added to array thumbnails
cell.textLabel.text = self.thumbnails[indexPath.row];
return cell;
}
#end
.h file
#import <UIKit/UIKit.h>
#interface SimpleTableViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
#property (strong, nonatomic) NSMutableArray *thumbnails;
#end
Try
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *path = [[NSBundle mainBundle] pathForResource:#"food" ofType:#"plist"];
NSArray *tableData = [[NSArray alloc]initWithContentsOfFile:path];
NSMutableArray *thumbnails = [NSMutableArray array];
for (NSDictionary *dict in tableData){
NSLog(#"%#",dict); // prints all key value pairs in dictionary
[thumbnails addObject:dict[#"name"];
}
NSLog(#"outside %#", thumbnails); // this prints last value added to thumbnails array
}
EDIT :
#property (nonatomic, strong) NSMutableArray *thumbnails;
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *path = [[NSBundle mainBundle] pathForResource:#"food" ofType:#"plist"];
NSArray *tableData = [[NSArray alloc]initWithContentsOfFile:path];
self.thumbnails = [NSMutableArray array];
for (NSDictionary *dict in tableData){
NSLog(#"%#",dict); // prints all key value pairs in dictionary
[self.thumbnails addObject:dict[#"name"];
}
NSLog(#"outside %#", self.thumbnails); // this prints last value added to thumbnails array
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//Initialize Cell
cell.textLabel.text = self.thumbnails[indexPath.row];
return cell;
}
There are several mishaps with your code.
A. Looking at the provided plist data, you have an array of dictionaries.
B. This line, allocated thumbnails as a local NSArray.
NSArray *thumbnails = [[NSArray alloc] init];
But this line, what is self.thumbnails which is a property? It is not thumbnails above. And it looks lie key #"name" contains an array rather than a final object.
self.thumbnails = [dict objectForKey:#"name"];

Resources