我是iOS開發新手。目前,我試圖從.plist文件中獲取數據並在UITableView上顯示。在運行應用程序時,Xcode沒有提供任何錯誤,但我的應用程序說明不了什麼,但這iOS從.plist文件加載數據
這裏是我的代碼
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>
我沒能找出問題。所以,我的問題是我的代碼中有什麼問題?如何顯示plist文件中的數據?我瀏覽了前面的問題在stackoverflow中,但這並沒有幫助我。
numberOfSectionsInTableView你正在返回0嗎? – Bamsworld
@Yasir你需要解析plist數據,然後將其加載到tableview中。檢查你的數組是否有信息或沒有。使用Json解析器 –