繁体   English   中英

将Plist读入UITableView时遇到问题

[英]Having issue with reading Plist into UITableView

我试图在UITableView中显示字典plist,我在这里阅读了很多问题和答案,但没有运气! 好像单元格返回null! 这是我的plist: 在此输入图像描述

和我的代码:

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

}

在此输入图像描述

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM