IOS中从服务器上加载数据,以Group形式再次分析数组
2013-08-19 16:40
543 查看
//从服务器上加载数据 -(void) loadDataFromServer { NSString *strurl =[NSStringstringWithFormat:@"%@/menus.php",HTTP_SERVER]; NSURL *url = [NSURL URLWithString:strurl]; ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; NSMutableDictionary *postDic = [[NSMutableDictionary alloc] init]; //getMenusList 请求数据 [postDic setObject:@"getMenusList" forKey:@"action"]; NSData *postData = [NSJSONSerialization dataWithJSONObject:postDicoptions:NSJSONWritingPrettyPrinted error:nil]; //data是字符串 NSString *postString = [[NSString alloc] initWithData:postDataencoding:NSUTF8StringEncoding];//把data格式的postdata转换成string格式 [request setPostValue:postString forKey:@"data"]; //传值 [request setCompletionBlock:^{ //阻塞块可以吧整个函数作为变量,传到setCompletionBlock中 NSDictionary *dic = [NSJSONSerializationJSONObjectWithData:request.responseDataoptions:NSJSONReadingMutableContainers error:nil]; //把服务器返回的json字符串的data转化成dictionary if([[dic objectForKey:@"statusID"] isEqualToString:@"0"]) { NSArray *arr = [dic objectForKey :@"reArray"]; self.tableArray = [NSMutableArray arrayWithArray:arr]; [self startDownLoadImageFromServer]; [self addDish]; } else { NSString *msg = [dic objectForKey:@"msg"]; UIAlertView *alertV = [[UIAlertView alloc] initWithTitle:@"提示"message:msg delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil]; [alertV show]; [alertV release]; } [_tableView reloadData]; NSLog(@"%@",request.responseString); }]; //返回数据 //下载失败后跳入 [request setFailedBlock:^{ }]; [request startAsynchronous]; [postString release]; } //开始下载图片 -(void) startDownLoadImageFromServer { for (NSDictionary *dic in self.tableArray) { NSString *imgurl = [dic objectForKey:@"MImage"]; NSString *imageUrlStr = [NSStringstringWithFormat:@"%@/%@",HTTP_SERVER,imgurl];//HTTP_SERVER是定义的宏,imageUrlStr是服务器上图片的地址 NSURL *url = [NSURL dc54 URLWithString:imageUrlStr]; ASIHTTPRequest *requst = [ASIHTTPRequest requestWithURL:url]; [requst setCompletionBlock:^{ UIImage *imageData = [UIImage imageWithData:requst.responseData]; if (!imageData) { return; } NSMutableDictionary *dic2 = [NSMutableDictionarydictionaryWithDictionary:dic]; [dic2 setObject:imageData forKey:@"image2"]; // int inx = [self.tableArray indexOfObject:dic]; NSArray *arr =[self.tableDic allKeys]; for (NSString *key in arr) { NSMutableArray *arr1=[self.tableDic objectForKey:key]; int inx=[arr1 indexOfObject:dic]; if(inx != NSNotFound) { [arr1 replaceObjectAtIndex:inxwithObject:dic2];//用dic2代替数组里面下标为inx的对象 } } [self.tableView reloadData]; // if(inx != NSNotFound) //如果找到inx(因为刷新后tablearray里的地址变了,inx就为-1,说明没找到) // { // [self.tableArray replaceObjectAtIndex:inx withObject:dic2]; // //user.userImage = [UIImage imageWithData:requst.responseData];//responseData就是图片数据 // [_tableView reloadData]; // } }];//请求下载 [requst startAsynchronous]; } } //再次分析菜单的类型(用的group索引)(一个key一个数组) -(void)addDish { for(int i=0;i<self.tableArray.count;i++) { NSArray *keysArray=[self.tableDic allKeys]; NSString *str=[[self.tableArray objectAtIndex:i]objectForKey:@"MType"]; if([keysArray indexOfObject:str]!=NSNotFound) { NSMutableArray *arr=[self.tableDic objectForKey:str]; [arr addObject:[self.tableArray objectAtIndex:i]]; } else { NSMutableArray *arr=[NSMutableArray array]; [arr addObject:[self.tableArray objectAtIndex:i]]; [self.tableDic setObject:arr forKey:str]; } } } |
相关文章推荐
- iOS开发-post请求向服务器发送json格式数据(数组或字典)
- IOS用AFN发送字符串形式的Json数据给服务器实例
- 前端jsp以数组形式提交数据给服务器
- iOS开发-post请求向服务器发送json格式数据(数组或字典)
- Ext form加载数据 load方法 load加载数据要是数组形式的,不能是json对象
- TFS文件系统数据服务器启动加载分析
- 用数据分析各移动广告投放渠道背后的规律| 投放渠道VS广告投放主行业VS广告形式
- Bootstrap-Table实现从服务器加载数据进行显示
- Microsoft Excel 2010 - 加载数据分析工具
- 从recordset对象提取数组形式数据,
- IOS之分析网易新闻存储数据(CoreData的使用,增删改查)
- Android异步加载学习笔记之一:用AsyncTask加载服务器json数据
- 再次谈谈easyui datagrid 的数据加载
- unserialize的这个问题是由一个emlog论坛用户在使用时报错而发现的 问题表现情况如下: emlog缓存的保存方式是将php的数据对象(数组)序列化(serialize)后以文件的形式存放,
- iOS 异步图片加载优化与常用开源库分析
- ios网络学习------4 UIWebView的加载本地数据的三种方式
- iOS 学习日志 : 向服务器发送Json数据
- 利用python进行数据分析-数据加载、存储与文件格式1
- iOS实现从服务器请求json数据并转化成NSDictionary
- [PHP]利用XAMPP搭建本地服务器, 然后利用iOS客户端上传数据到本地服务器中(二.配置MySQL数据库)