在选择表格单元格“didSelect”时如何将图像解析到另一个视图?

时间:2014-09-18 10:05:04

标签: ios iphone uitableview afnetworking

我收到了来自网络服务的回复并在我的应用程序中使用AFNetworking

{
"CityMap_Image":
 {"
 ImagePath1":"alm.demo11.com\/admin\/AlMithaqImages\/24378070-98a4-4d05-9306-d17b0c6c58c9.jpg",
"ImagePath2":"alm.demo11.com\/admin\/AlMithaqImages\/ad9f24f9-ca58-4c73-8289-161b5d5bd16d.jpg"
}
}

MainView中的表配置

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
CityDetailViewController * cityMap = [[CityDetailViewController alloc]initWithNibName:@"CityDetailViewController" bundle:nil];

if (tableView == stage3menuTableView) {

    if (indexPath.row == 0) {


        [self.navigationController pushViewController:cityMap animated:NO];
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
            [manager GET:@"http://alm.demo11.com/API.svc/get_CityMap" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {

                self.nDic = (NSDictionary *)responseObject;
                NSMutableDictionary *nDic1 = [[NSMutableDictionary alloc]initWithDictionary:[self.nDic objectForKey:@"CityMap_Image"]];
                NSString *img = [NSString stringWithFormat:@"http://%@",[nDic1 objectForKey:@"ImagePath1"]];
                NSURL *url = [NSURL URLWithString:img];
                NSData *data = [NSData dataWithContentsOfURL:url];

                cityMap.passedImage = [UIImage imageWithData:data];
                NSLog(@"response dateJSON: %@", cityMap.passedImage);


            } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                UIAlertView *erroralert = [[UIAlertView alloc] initWithTitle:@"Message" message:@"Sorry, Server is not responding. Please try later" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
                [erroralert show];

            }];



    }


    else if (indexPath.row == 1)
    {

        [self.navigationController pushViewController:cityMap animated:NO];
        cityMap.passedImage = [UIImage imageNamed:@"morebg.png"];


    }else
    {

        [self.navigationController pushViewController:cityMap animated:NO];
        cityMap.passedImage = [UIImage imageNamed:@"morebg.png"];

    }
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

}

SecondView

的.h

@interface DetailViewController : UIViewController<UIScrollViewDelegate>
{
IBOutlet UIImageView *bandImage;
}

@property (strong, nonatomic) UIImageView *bandImage;
@property (strong, nonatomic) IBOutlet UIScrollView *scrollView;
 @property (nonatomic, strong) UIImage *passedImage;

 @end

的.m

-(Void)ViewDidLoad
{
     bandImage.image = self.passedImage;

     }

对于静态图片,它运行正常,但对于网络服务,它无法正常工作..谁能说出我错在哪里?

1 个答案:

答案 0 :(得分:1)

将您的方法更新为:

 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    CityDetailViewController * cityMap = [[CityDetailViewController alloc]initWithNibName:@"CityDetailViewController" bundle:nil];

if (tableView == stage3menuTableView) {

       if (indexPath.row == 0) {



    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        [manager GET:@"http://alm.demo11.com/API.svc/get_CityMap" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {

            self.nDic = (NSDictionary *)responseObject;
            NSMutableDictionary *nDic1 = [[NSMutableDictionary alloc]initWithDictionary:[self.nDic objectForKey:@"CityMap_Image"]];
            NSString *img = [NSString stringWithFormat:@"http://%@",[nDic1 objectForKey:@"ImagePath1"]];
            NSURL *url = [NSURL URLWithString:img];
            NSData *data = [NSData dataWithContentsOfURL:url];

            cityMap.passedImage = [UIImage imageWithData:data];
            [self.navigationController pushViewController:cityMap animated:NO];
            NSLog(@"response dateJSON: %@", cityMap.passedImage);


        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            UIAlertView *erroralert = [[UIAlertView alloc] initWithTitle:@"Message" message:@"Sorry, Server is not responding. Please try later" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [erroralert show];

        }];



}


else if (indexPath.row == 1)
{


    cityMap.passedImage = [UIImage imageNamed:@"morebg.png"];
    [self.navigationController pushViewController:cityMap animated:NO];


}else
{


    cityMap.passedImage = [UIImage imageNamed:@"morebg.png"];
    [self.navigationController pushViewController:cityMap animated:NO];

}
[tableView deselectRowAtIndexPath:indexPath animated:YES];

}