2012-12-08 39 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