将Plist读入UITableView时遇到问题

时间:2013-05-13 08:31:20

标签: iphone ios objective-c xcode ipad

我正在尝试将字典plist显示到UITableView中,我在这里阅读了很多问题和答案,但没有运气!好像单元格返回null!这是我的plist: enter image description here

和我的代码:

-(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

4 个答案:

答案 0 :(得分: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;

}

答案 1 :(得分:0)

enter image description here

  1. 将根目录设为NSDictionary,将您的newsFeed放入字典中。
  2. 使用NSDictionary *dict = [NSDictionary ...fromFile:...];
  3. newsFeed = [dict valueForKey:@"some_key"];

答案 2 :(得分:0)

你应该学习如何调试这样的问题。或者,如果你尝试过,你应该告诉我们你在问题中尝试了什么。

viewDidLoad中设置断点,并确保正确加载了plist。路径可能是错误的。该文件可能已损坏或具有与您期望的不同的根对象。

numberOfSectionsInTableView中设置断点。它被召唤了吗?它是在你加载你的plist之前或之后调用的吗?可能之后。在这种情况下,您需要致电[tableView reloadData]。如果它被称为你返回什么。

继续这样,一步一步,直到找到原因。

答案 3 :(得分: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;
}