通过 Segue 将缓存图像传递给 DetailViewController
Passing Cache Image Via Segue to DetailViewController
我正在使用数据任务通过 JSON 文件显示来自外部服务器的文本数据。效果很好
我正在使用 AFNetworking 的文件 #import "UIImageView+AFNetworking.h"
从 Tableview 的 Cell 声明中提取的上述数据下载图像,它也工作得很好。
But when selecting a row to pass selected rows Image via Segue to another view controller it giving error,
这是我的 Tableview 单元格代码,它非常好并显示文本和图像数据
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"maincell" forIndexPath:indexPath];
Categories * CategoryObject;
CategoryObject = [categoryArray objectAtIndex:indexPath.row];
cell.textLabel.text = CategoryObject.categoryTitle;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
// Configure the cell Image...
NSURL *url = [NSURL URLWithString:CategoryObject.categoryImage];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
UIImage *placeholderImage = [UIImage imageNamed:@"placeholder"];
__weak UITableViewCell *weakCell = cell;
[cell.imageView setImageWithURLRequest:request placeholderImage:placeholderImage success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
weakCell.imageView.image = image;
[weakCell setNeedsLayout];
} failure:nil];
return cell;
}
现在看一下出现错误的 Segue 代码
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"showAnotherView"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
DetailImageViewController *destViewController = segue.destinationViewController;
destViewController.imageName = [ UIImageView objectAtIndex:indexPath.row.image];
}
}
我的代码出错
no known class method for selector objectAtIndex:
最后一行代码
destViewController.imageName = [ UIImageView objectAtIndex:indexPath.row.image];
我想要完成的事情是解决这个错误并传递上面通过 AFNetworking 下载的缓存图像,并在 segue 中传递该图像以显示在下一个视图控制器中。谁能帮帮我?
查看您的代码,似乎您正在访问 UIImageView
上的 objectAtIndex:
方法,除非您有类别 class 扩展 UIImageView
,否则没有该方法.如果你要处理原始选择,它应该是这样的,
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"showAnotherView"])
{
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
UITableViewCell * cell = [self.tableView cellForRowAtIndexPath:indexPath];
DetailImageViewController *destViewController = segue.destinationViewController;
destViewController.imageName = Cell.imageView.image;
}
}
希望对您有所帮助。
我正在使用数据任务通过 JSON 文件显示来自外部服务器的文本数据。效果很好
我正在使用 AFNetworking 的文件 #import "UIImageView+AFNetworking.h"
从 Tableview 的 Cell 声明中提取的上述数据下载图像,它也工作得很好。
But when selecting a row to pass selected rows Image via Segue to another view controller it giving error,
这是我的 Tableview 单元格代码,它非常好并显示文本和图像数据
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"maincell" forIndexPath:indexPath];
Categories * CategoryObject;
CategoryObject = [categoryArray objectAtIndex:indexPath.row];
cell.textLabel.text = CategoryObject.categoryTitle;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
// Configure the cell Image...
NSURL *url = [NSURL URLWithString:CategoryObject.categoryImage];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
UIImage *placeholderImage = [UIImage imageNamed:@"placeholder"];
__weak UITableViewCell *weakCell = cell;
[cell.imageView setImageWithURLRequest:request placeholderImage:placeholderImage success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
weakCell.imageView.image = image;
[weakCell setNeedsLayout];
} failure:nil];
return cell;
}
现在看一下出现错误的 Segue 代码
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"showAnotherView"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
DetailImageViewController *destViewController = segue.destinationViewController;
destViewController.imageName = [ UIImageView objectAtIndex:indexPath.row.image];
}
}
我的代码出错
no known class method for selector
objectAtIndex:
最后一行代码
destViewController.imageName = [ UIImageView objectAtIndex:indexPath.row.image];
我想要完成的事情是解决这个错误并传递上面通过 AFNetworking 下载的缓存图像,并在 segue 中传递该图像以显示在下一个视图控制器中。谁能帮帮我?
查看您的代码,似乎您正在访问 UIImageView
上的 objectAtIndex:
方法,除非您有类别 class 扩展 UIImageView
,否则没有该方法.如果你要处理原始选择,它应该是这样的,
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"showAnotherView"])
{
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
UITableViewCell * cell = [self.tableView cellForRowAtIndexPath:indexPath];
DetailImageViewController *destViewController = segue.destinationViewController;
destViewController.imageName = Cell.imageView.image;
}
}
希望对您有所帮助。