如何读取Plist数据到UITableView [英] how to read Plist data to UITableView

查看:86
本文介绍了如何读取Plist数据到UITableView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

如何从PList获取数据到UITableView?

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

I have a plist with Dictionary and numbers of strings per dictionary.show into the url below.and this list of items is in thousands in the plist.

现在想要将这些列表显示在tableview中

Now want to display these list into the tableview


现在如何在<$ c $中显示 plist c> UITableView

now how can i display this plist into the 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;
}


推荐答案

更新2: 您需要为 tableView 委托数据源 c $ c>在您的 xib ViewController

UPDATE 2: You need to set the delegate and datasource for your tableView in your xib or ViewController.

ViewController.h 文件中

@interface ViewController:UIViewController <UITableViewDelegate, UITableDataSource>

试试我为您编写的代码。

Try this code which I have written for you.

- (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:我见过你的 plist 在我的MAC中,我发现我们在中使用字典数组 A.plist

UPDATE 2: After I have seen your plist in my MAC, I have found out that we are working with array of dictionaries in your A.plist.

所以我发现代码本身有一个错误。不在 plist 文件中,您也可以使用 8000数据plist ..它也可以工作。我完全退房了。现在您可以获得上述代码并开始使用。

So I found there is a bug in our code itself. Not in the plist file and you can use your 8000 data plist too.. Its working too. I have checked out totally. Now you can get the above Code and start work with.

这篇关于如何读取Plist数据到UITableView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆