0

可能重复:
如何从 PList 获取数据到 UITableView?

我有一个带有字典的 plist 和每个字典的字符串数。显示到下面的 url 中。这个项目列表在 plist 中有数千个。

现在想将这些列表显示到表格视图中

进入图像 .

现在我怎么能把它显示plistUITableView

我正在尝试的是:

- (id)readPlist:(NSString *)fileName 
{

NSString *error;
NSPropertyListFormat format;
id plist;

NSString *localizedPath = [[NSBundle mainBundle] pathForResource:@"A" ofType:@"plist"];

dic =[NSDictionary dictionaryWithContentsOfFile:localizedPath];



plist = [NSPropertyListSerialization propertyListFromData:dic mutabilityOption:NSPropertyListImmutable format:&format errorDescription:&error];
if (!plist) {
    NSLog(@"Error reading plist from file '%s', error = '%s'", [localizedPath UTF8String], [error UTF8String]);
    [error release];
}

return plist;
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

dict =[self readPlist:@"A"];
return dict.allKeys.count;
 }

 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
 {

dict = [self readPlist:@"A"];
 key = [dict.allKeys objectAtIndex:section];
return [[dict valueForKey:key] 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] autorelease];
}

  cell.textLabel.text = [[dict objectForKey:key] objectAtIndex:indexPath.row];

return cell;
}
4

2 回答 2

1

将 Plist 数据存储在数组中

- (id)readPlist:(NSString *)fileName 
{

NSString *error;
NSPropertyListFormat format;
id plist;

NSString *localizedPath = [[NSBundle mainBundle] pathForResource:@"A" ofType:@"plist"];

// declare your array in .h file
  array = [NSArray arrayWithContentsOfFile:localizedPath];    

plist = [NSPropertyListSerialization propertyListFromData:dic mutabilityOption:NSPropertyListImmutable format:&format errorDescription:&error];
if (!plist) {
    NSLog(@"Error reading plist from file '%s', error = '%s'", [localizedPath UTF8String], [error UTF8String]);
    [error release];
}

return plist;
}

然后写在表中

- (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] autorelease];
}

  cell.textLabel.text = [array objectAtIndex:indexPath.row] valueForKey:@"keyname"];;

return cell;
}
于 2012-12-08T09:51:51.893 回答
1

更新 2:您需要在您的or中设置delegateand 。datasourcetableViewxibViewController

在你的ViewController.h文件中

@interface ViewController:UIViewController <UITableViewDelegate, UITableDataSource>

试试我为你写的这段代码。

- (void)viewDidLoad {

    tableView.delegate = self;
    tableView.dataSource = self;

    NSString *path = [[NSBundle mainBundle] pathForResource:@"Filename" ofType:@"plist"];
    NSArray *contentArray = [NSArray arrayWithContentsOfFile:path];
    // Having outlet for tableArray variable.
    tableArray = [[NSMutableArray alloc]initWithArray:contentArray copyItems:YES];

    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (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 = @"MyCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
    }
    NSDictionary *dictionary = [tableArray objectAtIndex:indexPath.section];
    NSArray *keysArray = dictionary.allKeys;
    // This below line display the key in textLabel
    cell.textLabel.text = [keysArray objectAtIndex:indexPath.row];
    // Below line will display the value of a key in detailTextLabel.
    cell.detailTextLabel.text = [dictionary valueForKey:[keysArray objectAtIndex:indexPath.row]];
    return cell;
}

更新 2:在我的 MAC 中看到您之后plist,我发现我们正在使用array of dictionaries您的A.plist.

在此处输入图像描述

所以我发现bug我们的代码本身就有一个。不在plist文件中,您可以使用您的8000 data plist too.. 它也可以工作。我已经彻底检查过了。现在您可以获得上述代码并开始使用。

于 2012-12-08T10:35:38.197 回答