在阅读Plist到UITableView时遇到问题
我想在UITableView中显示一个字典plist,我在这里阅读了许多问题和答案,但没有运气!它似乎细胞返回null!这里是我的plist: 在阅读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;
}
,结果是什么:
试一下这个在你想只显示标题那就试试这个
- (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;
}
什么是newsfeed的NSLog? – Rajneesh071 2013-05-13 08:42:20
首先清除你的字典和数组的概念... – Rajneesh071 2013-05-13 09:13:04
试试这个字典= [newsFeed objectAtIndex:indexPath。部分]; cell.textLabel.text = [dictionary objectForKey:@“title”]; – Rajneesh071 2013-05-13 09:14:17
- 设置root作为
NSDictionary
,放您的newsFeed
在字典中。 - 使用
NSDictionary *dict = [NSDictionary ...fromFile:...];
newsFeed = [dict valueForKey:@"some_key"];
你会修改你的代码吗? – 2013-05-13 08:44:26
你应该学会如何调试这样的问题。或者如果你尝试了,你应该告诉我们你在这个问题上所做的尝试。
在viewDidLoad
中设置一个断点,并确保plist加载正确。路径可能是错误的。该文件可能已损坏或具有不同的根对象,那么你期望的。
在numberOfSectionsInTableView
中设置断点。它是否被称为?它是在加载你的plist之前还是之后调用的?可能在之后。在这种情况下,您需要拨打[tableView reloadData]
。如果它被称为你回报什么。
继续这样,一步一步走,直到找到原因。
试试这个,我使用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;
}
什么在字典中的价值? – Rajneesh071 2013-05-13 08:34:16
@ Rajneesh071 NSDictionary * dictionary; – 2013-05-13 08:34:35
我在问价值,你在哪里设定价值 – Rajneesh071 2013-05-13 08:35:58