2013-05-13 120 views
0

我想在UITableView中顯示一個字典plist,我在這裏閱讀了許多問題和答案,但沒有運氣!它似乎細胞返回null!這裏是我的plist: enter image description here在閱讀Plist到UITableView時遇到問題

和我的代碼:

-(void)viewDidLoad { 


    NSString *path = [[NSBundle mainBundle] pathForResource:@"feed" ofType:@"plist"]; 
    newsFeed = [NSArray arrayWithContentsOfFile:path]; 
} 


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return newsFeed.count; 
} 


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

    return dictionary.allKeys.count; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    dictionary = [newsFeed objectAtIndex:indexPath.section]; 
    cell.textLabel.text = [[newsFeed objectAtIndex:indexPath.row] valueForKey:@"title"]; 

    return cell; 

} 

,結果是什麼:

enter image description here

+0

什麼在字典中的價值? – Rajneesh071 2013-05-13 08:34:16

+0

@ Rajneesh071 NSDictionary * dictionary; – 2013-05-13 08:34:35

+0

我在問價值,你在哪裏設定價值 – Rajneesh071 2013-05-13 08:35:58

回答

2

試一下這個在你想只顯示標題那就試試這個

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 1; 
} 
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
    { 

     return newsFeed.count; 
    } 

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 

     dictionary = [newsFeed objectAtIndex:indexPath.row]; 
     cell.textLabel.text = [dictionary valueForKey:@"title"]; 

     return cell; 

    } 

編輯
如果要顯示所有記錄,那麼試試這個

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return newsFeed.count; 
} 


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

    return dictionary.allKeys.count; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    dictionary = [newsFeed objectAtIndex:indexPath.section]; 

    NSString *key =[[dictionary allKeys] objectAtIndex:indexPath.row]; 

    cell.textLabel.text = [dictionary valueForKey:key]; 

    return cell; 

} 
+0

什麼是newsfeed的NSLog? – Rajneesh071 2013-05-13 08:42:20

+0

首先清除你的字典和數組的概念... – Rajneesh071 2013-05-13 09:13:04

+1

試試這個字典= [newsFeed objectAtIndex:indexPath。部分]; cell.textLabel.text = [dictionary objectForKey:@「title」]; – Rajneesh071 2013-05-13 09:14:17

0

enter image description here

  1. 設置root作爲NSDictionary,放您的newsFeed在字典中。
  2. 使用NSDictionary *dict = [NSDictionary ...fromFile:...];
  3. newsFeed = [dict valueForKey:@"some_key"];
+0

你會修改你的代碼嗎? – 2013-05-13 08:44:26

0

你應該學會如何調試這樣的問題。或者如果你嘗試了,你應該告訴我們你在這個問題上所做的嘗試。

viewDidLoad中設置一個斷點,並確保plist加載正確。路徑可能是錯誤的。該文件可能已損壞或具有不同的根對象,那麼你期望的。

numberOfSectionsInTableView中設置斷點。它是否被稱爲?它是在加載你的plist之前還是之後調用的?可能在之後。在這種情況下,您需要撥打[tableView reloadData]。如果它被稱爲你回報什麼。

繼續這樣,一步一步走,直到找到原因。

0

試試這個,我使用plist的很多。

在viewDidLoad中

NSString *plistPath = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:self.plistName]; 

self.tableContents=[NSDictionary dictionaryWithContentsOfFile:plistPath]; 

NSLog(@"table %@",self.tableContents); 
NSLog(@"table with Keys %@",[self.tableContents allKeys]); 

self.sortedKeys = [[self.tableContents allKeys] sortedArrayUsingSelector:@selector(localizedStandardCompare:)]; 


- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section { 
    NSArray *listData =[self.tableContents objectForKey:[self.sortedKeys objectAtIndex:section]]; 
    return [listData count]; 
} 


- (UITableViewCell *)tableView:(UITableView *)tableView 
     cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier"; 

    NSArray *listData =[self.tableContents objectForKey:[self.sortedKeys objectAtIndex:[indexPath section]]]; 

    UITableViewCell * cell = [tableView 
           dequeueReusableCellWithIdentifier:SimpleTableIdentifier]; 

    if(cell == nil) { 
     cell = [[UITableViewCell alloc] 
       initWithStyle:UITableViewCellStyleDefault 
       reuseIdentifier:SimpleTableIdentifier]; 
    } 

    cell.selectionStyle=UITableViewCellSelectionStyleNone; 

    NSUInteger row = [indexPath row]; 

    cell.textLabel.text = [NSString stringWithFormat:@" %@",[listData objectAtIndex:row]]; //take your object out 

return cell; 
}