您的位置:首页 > 移动开发 > IOS开发

IOS之从plist文件中获取数据

2017-04-01 02:42 260 查看
               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;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: