[英]Load data from a plist to a UITableView
我正在開發一個使用Storyboard的iPhone應用程序。 第一個場景,我有一個Table View Controller。 我有一個屬性文件名稱公司,並且我正在嘗試加載要在UITableView中顯示的文件的值。 我已經待了幾個小時了,但無濟於事。 這個問題接近了,但是答案也沒有幫助我。
這是屬性文件的格式。
這是我的代碼。
CompaniesTableViewController.m文件
#import "CompaniesTableViewController.h"
@interface CompaniesTableViewController ()
@end
@implementation CompaniesTableViewController
NSMutableArray *companies;
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *companiesFile = [[NSBundle mainBundle]pathForResource:@"companies" ofType:@"plist"];
companies = [[NSMutableArray alloc]initWithContentsOfFile:companiesFile];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [companies count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.textLabel.text = [[companies objectAtIndex:indexPath.row]objectForKey:@"Value"];
return cell;
}
不會填充表視圖。 有人可以告訴我我所錯過的一切嗎?
首先,您必須分配並初始化UITableViewCell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = [[companies objectAtIndex:indexPath.row]objectForKey:@"Value"];
return cell;
}
理想情況下,您的plist應該有一個包含一個公司鍵的字典,其值應該是NSArray。
您讀的字典不是數組。
pList中的條目類型是什么。 您能否從您的pList中獲取公司名稱。 嘗試通過公司的價值鍵NSLogging公司名稱。
NSString *path = [[NSBundle mainBundle] pathForResource:@"companies" ofType:@"plist"];
NSData *plistData = [NSData dataWithContentsOfFile:path];
NSString *error; NSPropertyListFormat format;
NSArray *companyData = [NSPropertyListSerialization propertyListFromData:plistData
mutabilityOption:NSPropertyListImmutable
format:&format
errorDescription:&error];
companies= [[NSMutableArray alloc] initWithArray:companyData];
NSLog(@"companyData:%@",companyData);
嗨首先,plist文件的結構必須是這樣的(僅顯示其中一部分)
然后是我放置的位置(總裁是根文件夾)
然后我用來讀取數據的代碼是(總裁是NSArray
)
加載數據后,您需要重新加載tableview。
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *companiesFile = [[NSBundle mainBundle]pathForResource:@"companies" ofType:@"plist"];
companies = [[NSMutableArray alloc]initWithContentsOfFile:companiesFile];
[self.tableView reloadData];
}
為此,您必須創建一個NSDictonary。
@implementation CompaniesTableViewController
{
NSDictionary *dict;
NSArray *arayDict;
}
..
NSString *companiesFile = [[NSBundle mainBundle]pathForResource:@"companies" ofType:@"plist"];
//populate dicationary
dict = [[NSDictionary alloc] initWithContentsOfFile:companiesFile];
//populate array with allkeys from dict
arayDict = [dict allKeys];
-
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [arrayDict count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
NSString *key = [arayDict objectAtIndex:indexPath.row];
NSDictionary *dictionary = [dict objectForKey:key];
cell.textLabel.text =[NSString stringWithFormat:@"%@", dictionary];
return cell;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.