• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 如何读取 plist 文件然后填充 UItableView?

[复制链接]
菜鸟教程小白 发表于 2022-12-12 09:25:35 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我一直在尝试从 plist 文件中读取数据。这就是它的结构

|term|detail(string)|

我的属性:

@property (nonatomic, strong) NSArray *terms;
@property (nonatomic, strong) NSArray *termKeys;//this is just a array to keep track
@property (nonatomic, strong) NSString *detail;

这就是我访问 cellForRowAtIndexPath

中详细信息的方式
 - (UITableViewCell *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)indexPath{
    UITableViewCell *cell = [[UITableViewCell alloc]
                             initWithStyle:UITableViewCellStyleDefault reuseIdentifier"cell"];

    NSString *currentTermsName = [termKeys objectAtIndex :[indexPath row]];
                                  [[cell textLabel] setText:currentTermsName];
    cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;


    detail = [terms objectAtIndex:indexPath.row]; 

    NSLog(@" bombs %@",terms[@"Bomb Threats"]);
    return cell;

}

在 View 中我有

- (void)viewDidLoad
    {
        [super viewDidLoad];


        NSString *myfile = [[NSBundle mainBundle]
                            pathForResource"terms" ofType"plist"];
        terms = [[NSDictionary alloc] initWithContentsOfFile:myfile];
        termKeys = [terms allKeys];
    }

它访问值,但它为每个对象存储相同的值假设我在 plist 中有 5 条不同的记录,如果我打印详细信息,它会显示相同的记录 5 次。

一旦设置了详细信息,然后我将其传递给 detialView

- (void) prepareForSegueUIStoryboardSegue *)segue senderid)sender{
    if([segue.identifier isEqualToString"detailsegue"]){
        TermsDetailViewController *controller = (TermsDetailViewController *)segue.destinationViewController;
        controller.detailTerm = detail;
    }
}

最后:

- (void)tableViewUITableView *)tableView didSelectRowAtIndexPathNSIndexPath *)indexPath
{
    [self performSegueWithIdentifier"detailsegue" sender:self];
}

这是我的字典:http://pastebin.com/bxAjJzHp

目标是将详细信息传递给 detailviewcontroller,就像 Master/detail 示例项目一样。



Best Answer-推荐答案


您不能在 cellForRowAtIndexPath: 方法中设置 detail 变量,因为这样值会变成 transient :它取决于用户如何滚动表格,而不是取决于用户点击了什么披露按钮。

移动这条线

detail = [terms objectAtIndex:indexPath.row];

didSelectRowAtIndexPath: 在调用 performSegueWithIdentifier: 之前解决问题。现在 detail 设置为响应用户在 prepareForSegue: 调用之前的点击,确保将正确的值与详细信息数据一起传递给 View Controller 。

关于ios - 如何读取 plist 文件然后填充 UItableView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15210906/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap