//从服务器上加载数据 -(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 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]; } }
}
|