IOS:OC--解析菜谱

1.caipu.txt
{
"data": [
{
"collect_num": "1622",
"food_str": "大虾 葱 生姜 植物油 料酒",
"id": "8289",
"num": 1622,
"pic": "http://www.qubaobei.com/ios/cf/uploadfile/132/9/8289.jpg",
"title": "油焖大虾"
},
{
"collect_num": "1565",
"food_str": "猪肉 青蒜 青椒 红椒 姜片",
"id": "2127",
"num": 1565,
"pic": "http://www.qubaobei.com/ios/cf/uploadfile/132/3/2127.jpg",
"title": "四川回锅肉"
},
{
"collect_num": "1486",
"food_str": "QQ糖 牛奶 芒果",
"id": "30630",
"num": 1486,
"pic": "http://www.qubaobei.com/ios/cf/uploadfile/132/31/30630.jpg",
"title": "超简单芒果布丁"
},
{
"collect_num": "1398",
"food_str": "鲜鱼 姜 葱 蒜 花椒",
"id": "9073",
"num": 1398,
"pic": "http://www.qubaobei.com/ios/cf/uploadfile/132/10/9073.jpg",
"title": "家常红烧鱼"
},
{
"collect_num": "1385",
"food_str": "豆腐 新鲜红椒 青椒 葱花 油",
"id": "10097",
"num": 1385,
"pic": "http://www.qubaobei.com/ios/cf/uploadfile/132/11/10097.jpg",
"title": "家常煎豆腐"
},
{
"collect_num": "1321",
"food_str": "瘦猪肉 生菜 豆瓣酱 干辣椒 花椒",
"id": "10509",
"num": 1321,
"pic": "http://www.qubaobei.com/ios/cf/uploadfile/132/11/10509.jpg",
"title": "水煮肉片"
},
{
"collect_num": "1229",
"food_str": "银耳 苹果 红糖",
"id": "46968",
"num": 1229,
"pic": "http://www.qubaobei.com/ios/cf/uploadfile/132/47/46968.jpg",
"title": "红糖苹果银耳汤"
},
{
"collect_num": "1191",
"food_str": "豆腐 肉末 生抽 白糖 芝麻油",
"id": "10191",
"num": 1191,
"pic": "http://www.qubaobei.com/ios/cf/uploadfile/132/11/10191.jpg",
"title": "麻婆豆腐"
},
{
"collect_num": "1133",
"food_str": "大米 皮蛋 猪肉 油条 香葱",
"id": "2372",
"num": 1133,
"pic": "http://www.qubaobei.com/ios/cf/uploadfile/132/3/2372.jpg",
"title": "皮蛋瘦肉粥"
},
{
"collect_num": "1115",
"food_str": "红薯粉 肉 姜 蒜 花椒",
"id": "2166",
"num": 1115,
"pic": "http://www.qubaobei.com/ios/cf/uploadfile/132/3/2166.jpg",
"title": "蚂蚁上树"
},
{
"collect_num": "1056",
"food_str": "猪肉 红椒 黄椒 洋葱 蛋清",
"id": "2262",
"num": 1056,
"pic": "http://www.qubaobei.com/ios/cf/uploadfile/132/3/2262.jpg",
"title": "糖醋肉"
},
{
"collect_num": "987",
"food_str": "豆腐 木耳 胡萝卜 香葱 番茄酱",
"id": "9971",
"num": 987,
"pic": "http://www.qubaobei.com/ios/cf/uploadfile/132/10/9971.jpg",
"title": "鱼香豆腐"
},
{
"collect_num": "978",
"food_str": "四季豆 干辣椒 蒜头 酱油 糖",
"id": "10172",
"num": 978,
"pic": "http://www.qubaobei.com/ios/cf/uploadfile/132/11/10172.jpg",
"title": "干煸四季豆"
},
{
"collect_num": "888",
"food_str": "胡萝卜 肉 蛋 生抽 盐",
"id": "2685",
"num": 888,
"pic": "http://www.qubaobei.com/ios/cf/uploadfile/132/3/2685.jpg",
"title": "胡萝卜肉末蒸蛋"
},
{
"collect_num": "873",
"food_str": "青辣椒 大蒜 香醋 白糖 生抽",
"id": "9972",
"num": 873,
"pic": "http://www.qubaobei.com/ios/cf/uploadfile/132/10/9972.jpg",
"title": "虎皮青椒"
},
{
"collect_num": "787",
"food_str": "排骨 李锦记叉烧酱 植物油 清水 油菜",
"id": "10437",
"num": 787,
"pic": "http://www.qubaobei.com/ios/cf/uploadfile/132/11/10437.jpg",
"title": "叉烧排骨"
},
{
"collect_num": "743",
"food_str": "面条 肉丝 淀粉 酱油 辣椒",
"id": "2348",
"num": 743,
"pic": "http://www.qubaobei.com/ios/cf/uploadfile/132/3/2348.jpg",
"title": "麻辣肉丝面"
},
{
"collect_num": "741",
"food_str": "黑木耳 玉米 牛蒡 胡萝卜 西兰花",
"id": "2892",
"num": 741,
"pic": "http://www.qubaobei.com/ios/cf/uploadfile/132/3/2892.jpg",
"title": "“五行”彩蔬汤"
},
{
"collect_num": "738",
"food_str": "土豆 翅根 葱 姜 料酒",
"id": "10044",
"num": 738,
"pic": "http://www.qubaobei.com/ios/cf/uploadfile/132/11/10044.jpg",
"title": "土豆炖翅根"
},
{
"collect_num": "737",
"food_str": "黄豆 红豆 绿豆 黑豆 黑米",
"id": "33783",
"num": 737,
"pic": "http://www.qubaobei.com/ios/cf/uploadfile/132/34/33783.jpg",
"title": "美人豆浆"
}
],
"ret": 1
}
ViewController.m
@interface ViewController ()
@property(nonatomic,strong)UITableView * tableView;
//字典
@property(nonatomic,strong)NSDictionary * dict;
@property(nonatomic,strong)NSMutableArray *dataarray;

  • (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    // self.tableView.backgroundColor=[UIColor redColor];

    self.tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:(UITableViewStylePlain)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor cyanColor];
    [self.view addSubview:self.tableView];

    //注册cell
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];

    //解析JSON(movie)wend
    [self Movie];

}
-(void)Movie{

//获取文档路径
NSString * filepath = [[NSBundle mainBundle] pathForResource:@"caipu" ofType:@"txt"];
//转化为data
NSData * fileData = [NSData dataWithContentsOfFile:filepath];
//解析
NSDictionary * dic1 = [NSJSONSerialization JSONObjectWithData:fileData options:NSJSONReadingAllowFragments error:nil];
//创建字典

self.dict = [NSMutableDictionary dictionaryWithDictionary:dic1];
NSLog(@"-----%@",self.dict);

// self.dataarray = [dic1 objectForKey:@"data"];
// NSLog(@"%@",self.dataarray);
//
// for(int i=0;){
//
// }

}

pragma mark -- 代理方法:

//分区

  • (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    //根据数组元素来返回
    return self.dict.count;
    }
    //分区的行数

  • (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

return 20;

}

//分区的返回cell

  • (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    //解析数据
    //拿到的是一个字典内部是一个数组
    NSArray * arr = [self.dict objectForKey:@"data"];

    //数组内部有字典需要拿到
    // NSArray * array = [_dict valueForKey:@"data"];

    NSLog(@"-----shuzuwei%@",arr);
    //拿到数组内部的字典
    NSDictionary * dic2 = [arr objectAtIndex:indexPath.row];
    cell.textLabel.text = [dic2 objectForKey:@"title"];

    return cell;

}

你可能感兴趣的:(IOS:OC--解析菜谱)