2013-05-13 3 views
0

Я пытаюсь отобразить словарь plist в UITableView, я прочитал много вопросов и ответов здесь, но не повезло! кажется, клетки возвращают 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

+0

@ Rajneesh071 NSDictionary * словарь; –

+0

Я спрашиваю о значении, где вы устанавливаете значение в нем – Rajneesh071

ответ

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

Что такое NSLog новостей? – Rajneesh071

+0

сначала очистите словарь и матрицу ... – Rajneesh071

+1

Попробуйте этот словарь = [newsFeed objectAtIndex: indexPath.раздел]; cell.textLabel.text = [словарь objectForKey: @ "title"]; – Rajneesh071

0

enter image description here

  1. Установите Root Как NSDictionary, положил ваш newsFeed в словаре.
  2. Использование NSDictionary *dict = [NSDictionary ...fromFile:...];
  3. newsFeed = [dict valueForKey:@"some_key"];
+0

, пожалуйста, измените свои коды? –

0

Вы должны научиться выявлять проблемы, как это. Или, если вы попытаетесь, вы должны сообщить нам, что вы пробовали в вопросе.

Установите точку останова в viewDidLoad и убедитесь, что plist загружен правильно. Путь может быть неправильным. Файл может быть поврежден или иметь другой корневой объект, а затем то, что вы ожидаете.

Установить контрольную точку в numberOfSectionsInTableView. Это вообще называется? Вызывается до или после загрузки вашего plist? Наверное, после. В этом случае вам необходимо позвонить [tableView reloadData]. Если это называется, то что вы возвращаете.

Продолжайте так, шаг за шагом, пока не найдете причину.

0

Попробуйте это, я используйте plist's много.

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