[IOS笔记]plist创建和使用
来源:互联网 发布:电脑桌面工作便签软件 编辑:程序博客网 时间:2024/06/07 14:13
【1】先代码创建plist文件
#import <Foundation/Foundation.h>int main(int argc, const char * argv[]) { @autoreleasepool { // insert code here... NSArray *dataArr = @[ @{@"name":@"单肩包", @"icon":@"danjianbao"}, @{@"name":@"钱包", @"icon":@"qianbao"}, @{@"name":@"链条包", @"icon":@"liantiaobao"}, @{@"name":@"手提包", @"icon":@"shoutibao"}, @{@"name":@"双肩包", @"icon":@"shuangjianbao"}, @{@"name":@"斜挎包", @"icon":@"xiekuabao"}]; BOOL flag = [dataArr writeToFile:@"/Users/cdj/DavidLong/program_iphone/Learning/ShoppingCartCreatPlist/ShoppingCartCreatPlist/shopData.plist" atomically:YES]; if (flag) { NSLog(@"写入成功"); } } return 0;}
【2】将plist文件放入到SupportingFiles中,然后读取使用
#import "ViewController.h"@interface ViewController ()@property (weak, nonatomic) IBOutlet UIView *shopCarView;@property (weak, nonatomic) IBOutlet UIButton *addButton;@property (weak, nonatomic) IBOutlet UIButton *removeButton;@property (nonatomic, strong) NSArray *gDataArr;@end@implementation ViewController-(NSArray *)gDataArr{ if(_gDataArr == nil){// 加载数据// 1获取全路径 NSString *dataPath = [[NSBundle mainBundle] pathForResource:@"shopData.plist" ofType:nil]; self.gDataArr = [NSArray arrayWithContentsOfFile:dataPath]; } return _gDataArr;}- (void)viewDidLoad { [super viewDidLoad];}//添加到购物车- (IBAction)add:(UIButton *)button { // 1.总列数 NSInteger allCols = 3;// 2.商品的宽度 CGFloat width = 80; CGFloat height = 100;// 3.求水平间距 CGFloat hMargin = (self.shopCarView.frame.size.width - allCols * width)/(allCols -1); CGFloat vMargin = (self.shopCarView.frame.size.height - 2 * height)/1;// 4.用subview的个数代替索引 NSInteger index = self.shopCarView.subviews.count; // 4.求出x值 CGFloat x = (hMargin + width) * (index % allCols); CGFloat y = (vMargin + height) * (index / allCols); // 5创建商品的view UIView *shopView = [[UIView alloc] init]; shopView.frame = CGRectMake(x, y, width, height); shopView.backgroundColor = [UIColor greenColor]; [self.shopCarView addSubview:shopView]; // 6创建商品的UIImageView对象 UIImageView *iconView = [[UIImageView alloc] init]; iconView.frame = CGRectMake(0, 0, width, width); iconView.backgroundColor = [UIColor purpleColor]; [shopView addSubview:iconView]; // 7创建商品的label对象 UILabel *titleLabel = [[UILabel alloc] init]; titleLabel.frame = CGRectMake(0, width, width, height - width); titleLabel.backgroundColor = [UIColor yellowColor]; titleLabel.textAlignment = NSTextAlignmentCenter; [shopView addSubview:titleLabel]; // 8设置数据 NSDictionary *dict = self.gDataArr[index]; iconView.image = [UIImage imageNamed:dict[@"icon"]]; titleLabel.text = dict[@"name"]; // 设置按钮的状态 if (index == 5) { button.enabled = NO; } // 删除按钮的状态 self.removeButton.enabled = YES;}//从购物车移除- (IBAction)remove:(UIButton *)button { // 1删除最后一个商品 UIView *lastShowView = [self.shopCarView.subviews lastObject]; [lastShowView removeFromSuperview]; // 2设置索引的值 self.addButton.enabled = YES; if (self.shopCarView.subviews.count == 0) { self.removeButton.enabled = NO; } }@end
阅读全文
0 0
- [IOS笔记]plist创建和使用
- iOS plist文件创建和读写
- 使用Xcode创建plist文件和使用
- iOS plist的创建
- iOS 使用plist和归档存储数据
- 使用plist创建动画
- 【iOS】plist的使用
- ios使用plist
- iOS Plist文件的创建
- 【代码笔记】iOS-城市plist
- IOS开发 笔记 ---plist篇
- 3.ios之NSBundle和plist的使用
- iOS-生成plist文件,在项目中代码创建plist
- iOS 创建.写入.读取plist文件
- iOS plist文件创建于读取
- iOS 创建.写入.读取plist文件
- iOS 创建.写入.读取plist文件
- iOS开发时读取plist文件创建和读取的方法
- css3 渐变
- Android_XListView请求网络数据
- spring学习十一 通过注解配置bean
- python3语言的基础学习
- 笔记本开机小键盘自动启动
- [IOS笔记]plist创建和使用
- Android_请求网络数据
- atitit 完整的知识体系表 学科体系表 v2
- MSSQL 存储过程加密解密
- 【bzoj1924】[Sdoi2010]所驼门王的宝藏(tarjan+STL+dp)
- 对使用动态SQL的几点建议
- 数据库常用操作(2)
- JSON字符串转换为对象
- 彻底解决Visual Studio 2010/2008编写C语言的问题