2012-12-08 41 views
0

可能重复:
How to Get Data from a PList into UITableView?如何读取的plist数据的UITableView

我有字典,每个dictionary.show串号一个的plist到URL below.and这个名单的物品在plist中的成千上万。

现在要显示这些清单到tableview中

eneter image

现在我怎么能显示到UITableView

plist我想做的是:

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

不要调用readPlist:方法所有的项目。取而代之的是,在接口文件中声明一个字典并使用它。因为它会像你说的那样造成性能问题,你的plist中有数以千计的数据。 –

+0

你是如何为你的tableView设置Delegate和Datasource? –

+0

你...我已经做到了 – Christien

回答

1

UPDATE 2:您需要在xibViewController中设置delegatedatasource作为tableView

在你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:我看过后,你plist在我的MAC,我发现,我们与array of dictionariesA.plist工作。

enter image description here

所以,我发现有在我们的代码本身就是一个bug。不在plist文件中,您可以使用您的8000 data plist too ..其工作方式也是如此。我已经完全检查了。现在你可以得到上面的代码并开始工作。

+0

没有帮助..表空是没有显示任何内容.....请帮助 – Christien

+0

亚,我已经完成了数据源和委托... donno ... wahts发生...在哪里放置nslog ?? – Christien

+0

你做了nsliog(“values%@”,tableDict);但它的空...意味着只有..values .. – Christien

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; 
} 
+0

@“keyname”将是关键权利?我使用noofrowsinsection和noofsectionintableview相同。但显示空白tableview – Christien

+0

你给行计数 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)部分 { return [array count]; } –

+0

雅做同..但空表 – Christien