Reputation: 8729
I am new to iOS development. Currently, I am trying to get data from a .plist file and showing on UITableView. While running the application, Xcode didn't provide any error, But my application shows nothing but this
Here is my code
TestAppTableViewController.h
#import <UIKit/UIKit.h>
@interface TestAppTableViewController : UITableViewController
@property (nonatomic,strong) NSArray *content;
@end
TestAppTableViewController.m
#import "TestAppTableViewController.h"
@interface TestAppTableViewController ()
@end
@implementation TestAppTableViewController
@synthesize content = _content;
- (NSArray *) content{
if (!_content) {
_content = [[NSArray alloc]initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"]];
}
return _content;
}
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 0;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [self.content count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Configure the cell...
cell.textLabel.text = [[self.content objectAtIndex:indexPath.row]valueForKey:@"city"];
cell.detailTextLabel.text = [[self.content objectAtIndex:indexPath.row]valueForKey:@"state"];
return cell;
}
Data.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
<dict>
<key>city</key>
<string>New York</string>
<key>state</key>
<string>NY</string>
<key>cityText</key>
<string>This is the description of the city that goes in the tex view just testing.</string>
</dict>
<dict>
<key>city</key>
<string>Los Angeles</string>
<key>state</key>
<string>CA</string>
<key>cityText</key>
<string>This the second item's textview</string>
</dict>
<dict>
<key>city</key>
<string>Chicago</string>
<key>state</key>
<string>IL</string>
<key>cityText</key>
<string>here is the text view description for the third item.</string>
</dict>
<dict>
<key>city</key>
<string>Sandiago</string>
<key>state</key>
<string>CA</string>
<key>cityText</key>
<string>Here is another city from california</string>
</dict>
<dict>
<key>city</key>
<string>Talahasy</string>
<key>state</key>
<string>FL</string>
<key>cityText</key>
<string>Talahasy is the capital of the florida state.</string>
</dict>
<dict>
<key>city</key>
<string>Boise</string>
<key>state</key>
<string>Idaho</string>
<key>cityText</key>
<string>boise is the capital of idaho and i have no idea where that is.</string>
</dict>
</array>
</plist>
I didn't able to figure out the problem. So, my question is what is the problem in my code ? How can I show the data from plist file ? I browsed the previous question's in stackoverflow but that didn't helped me.
Upvotes: 0
Views: 4181
Reputation: 23271
@interface ViewController:UIViewController <UITableViewDelegate, UITableDataSource>
just see your plist you are inside of array you can use dictionary. so implement like below code it help you
- (void)viewDidLoad {
tableView.delegate = self;
tableView.dataSource = self;
NSString *path = [[NSBundle mainBundle] pathForResource:@"yourFilename" ofType:@"plist"];
NSArray *contentArray = [NSArray arrayWithContentsOfFile:path];
// Having outlet for tableArray variable.
tableArray = [[NSMutableArray alloc]initWithArray:contentArray copyItems:YES];
[super viewDidLoad];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [tableArray count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// In your case dictionary contains strings with keys and values. The below line returns dictionary only. not array..
NSDictionary *dictionary = [tableArray objectAtIndex:section];
return dictionary.allKeys.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
}
NSDictionary *dictionaryOfArray = [tableArray objectAtIndex:indexPath.section];
NSArray *keysArray = dictionaryOfArray.allKeys;
cell.textLabel.text = [keysArray objectAtIndex:indexPath.row];
cell.detailTextLabel.text = [dictionaryOfArray valueForKey:[keysArray objectAtIndex:indexPath.row]];
return cell;
}
Upvotes: 0
Reputation: 5680
Your tableview needs at least one section-
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;// make this 1
}
also try using objectForKey instead like this (although its automatically invoked if key doesn't start with @ according to documentation) and type cast to be sure.
cell.textLabel.text = (NSString*)[[self.content objectAtIndex:indexPath.row] objectForKey:@"city"];
cell.detailTextLabel.text = (NSString*)[[self.content objectAtIndex:indexPath.row] objectForKey:@"state"];
If you are still getting nothing then it would be worth checking that content actually contains the plist data. In your -(NSArray *)content method, before you return it may be worth logging the count of _content. Also confirm that self.content = _content
Upvotes: 4
Reputation: 6952
I think the problem is here, you should return 1 or any number you want except 0:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 0;
}
Upvotes: 1