2015-05-26 1 views
0

У меня проблема с моим кодом, и я не могу понять, что не так.Заполнение Plist до UITableview

Мой код заключается в следующем:

@implementation FirstViewController { 
NSDictionary *countriesDetails; 
NSArray *countriesList; 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    NSURL *url = [[NSBundle mainBundle] URLForResource:@"countries" withExtension:@"plist"]; 
    countriesDetails = [NSDictionary dictionaryWithContentsOfURL:url]; 
    countriesList = countriesDetails.allKeys; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return countriesDetails.count; 
} 

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"]; 
    cell.textLabel.text = [[countriesList objectAtIndex:indexPath.row] objectForKey:@"country"]; 

    return cell; 
} 

Мой файл PLIST это:

<dict> 
<key>Paris</key> 
<dict> 
    <key>country</key> 
    <string>France</string> 
    <key>chName</key> 
    <string>Paris</string> 
    <key>chlink</key> 
    <string>www.paris.com</string> 
</dict> 
<key>Rome</key> 
<dict> 
    <key>country</key> 
    <string>Italy</string> 
    <key>chName</key> 
    <string>Rome</string> 
    <key>chlink</key> 
    <string>www.rome.com</string> 
</dict> 

Так что мое приложение будет иметь несколько tableviews, а первый показывает все страны у меня на PLIST ,

Мой XCode не показывает ошибки, но когда я бегу приложение он выходит из строя, и это выглядит:

2015-05-26 23: 34: 53.362 newApp [1511: 95592] - [__ NSCFString objectForKey: ]: непризнанный селектор, отправленный в экземпляр 0x7fde31f242d0 2015-05-26 23: 34: 53.366 neaApp [1511: 95592] * Завершение приложения из-за неперехваченного исключения «NSInvalidArgumentException», причина: '- [__ NSCFString objectForKey:]: нераспознанный селектор отправлен к примеру 0x7fde31f242d0 ' * Первый стек вызовов броска:

ответ

2

Ваш countriesList состоит из NSString s, а не NSDictionary s. Измените свой метод tableView:cellForRowAtIndexPath: на:

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"]; 
    cell.textLabel.text = [[countriesDetails objectForKey:[countriesList objectAtIndex:indexPath.row]] objectForKey:@"country"]; 

    return cell; 
}