在选择表格单元格“didSelect”时如何将图像解析到另一个视图?
Posted
技术标签:
【中文标题】在选择表格单元格“didSelect”时如何将图像解析到另一个视图?【英文标题】:On selecting the table cell "didSelect" how to parse images to another View? 【发布时间】:2014-09-18 10:05:03 【问题描述】:我正在从 Web 服务获得伙伴响应并在我的应用程序中使用 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"
主视图中的表格配置
-(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];
第二视图
.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】:将您的方法更新为:
-(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];
【讨论】:
感谢您的快速响应 :-) 它工作正常 .. 万岁 ..! 在 secondview.m 中应用断点并查看是否能够在第二个视图中获取该图像。检查passedImage 不应该为nil。以上是关于在选择表格单元格“didSelect”时如何将图像解析到另一个视图?的主要内容,如果未能解决你的问题,请参考以下文章
使用 didSelect 方法在 viewDidLoad 之后在单元格中显示图像
Swift:使用 tableView DidSelect 方法更改单元格的 UIButton 图像
在 UITableView 内的 UIImageView 中从 Web 加载图像