IOS之从plist文件中获取数据

本文介绍如何在iOS应用中使用Objective-C从plist文件读取数据,并将这些数据加载到对应的UI元素中,包括图片、价格等信息,并展示了如何通过NSAttributedString设置文本样式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


               1.定义数组

        NSMutableArray *_foodArray;

        2.从文件中读取plist文件路径(foodData是你plist文件的名字

        NSString *foodPlistPath =[[NSBundle mainBundle] pathForResource:@"foodData" ofType:@"plist"];

        3.初始化数组

        _foodArray = [[NSMutableArray alloc] initWithContentsOfFile:foodPlistPath];

        4.获取plist文件数据(image,newPrice,oldPrice为plist文件中的key

        NSString *imageName = [foodArray[i] objectForKey:@"image"];

        NSNumber *newPrice = [foodArray[i] objectForKey:@"newPrice"];

        NSNumber *oldPrice = [foodArray[i] objectForKey:@"oldPrice"];

        5.通过tag获取相关控件的一些属性

        UIImageView *imageView = (UIImageView *)[self viewWithTag:20+i];

        6.对imageView相关属性赋值

        imageView.image = [UIImage imageNamed:imageName];


        UILabel *newPriceLabel = (UILabel *)[self viewWithTag:50+i];

        newPriceLabel.text = [NSString stringWithFormat:@"%@",newPrice];


        UILabel *oldPriceLabel = (UILabel *)[self viewWithTag:70+i];

        NSString *oldStr = [NSString stringWithFormat:@"%@",oldPrice];

        //oldPrice的数据上设置中划线

        NSDictionary *attribtDic = @{NSStrikethroughStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle]};

        NSMutableAttributedString *attribtStr = [[NSMutableAttributedString alloc]initWithString:oldStr attributes:attribtDic];

        oldPriceLabel.attributedText = attribtStr;


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值