【问题标题】:how to read Plist data to UITableView [duplicate]如何将 Plist 数据读取到 UITableView [重复]
【发布时间】:2012-12-08 09:36:55
【问题描述】:

可能重复:
How to Get Data from a PList into UITableView?

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

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

.

现在我如何将这个plist 显示到UITableView

我正在尝试的是:

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

【问题讨论】:

  • 不要调用 readPlist: 方法的所有项目。取而代之的是,在接口文件中声明一个字典并使用它。因为它会像你说的那样产生性能问题,所以你的 plist 中有成千上万的数据。
  • 你是如何为你的 tableView 设置 Delegate 和 Datasource 的?
  • 是的...我已经这样做了
  • 我问你是怎么做到的?该委托方法是否被调用??
  • sorry 8000 item with 53 record in each item -> 8000*53 = 42400 records in your plist file..令人震惊..这真的很尴尬..:-{

标签: iphone ios ipad uitableview plist


【解决方案1】:

更新2:您需要在xibViewController 中为tableView 设置delegatedatasource

在您的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 后,我发现我们正在使用您的 A.plist 中的 array of dictionaries

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

【讨论】:

  • 没有帮助..table 是空的,没有显示任何东西......请帮助
  • 是的,我已经完成了数据源和委托...不要...正在发生的事情...将 nslog 放在哪里??
  • 是的,我做了 nsliog("values %@",tableDict);但它的到来是空的......只意味着......价值......
  • in viewdidload ,numberOfRowsInSection , numberOfSectionsInTableView 和 cellForRowAtIndexPath ...嘿,如果..它是一个短代码,你能给我发电子邮件吗。但对我来说是小鬼..我可以给你发邮件
  • @Christien 我完全重构了你的代码。你的代码没有问题。您的 plist 文件有问题。真的很难找到问题。我已经用我的替换了你的 plist 文件。有效。我认为 Xcode 无法从 plist 加载这么多数据。它甚至没有读取您的 plist 文件。对于 plist 文件,8000 条记录确实要大得多。最好使用数据库来存储数据。否则请在聊天室专家咨询..
【解决方案2】:

将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;
}

【讨论】:

  • @"keyname" 将是关键吗?我对 noofrowsinsection 和 noofsectionintableview 使用相同,但显示空白 tableview
  • 你有没有给出行数 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [array count]; }
  • 你做同样的事情..但是空表
  • 好吧,我在回答中做了一些更改。将 .plist 数据直接存储在数组中
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-06-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-27
  • 1970-01-01
相关资源
最近更新 更多