ImageView没有回到合适的位置

时间:2014-07-10 12:21:33

标签: ios objective-c uitableview

单击自定义表格单元格时,我使用以下代码显示图像。 但它显示黑屏,当我点击黑屏时它也没有返回到桌面视图。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    CustomTableViewCell *cell = tableView.visibleCells[indexPath.row];
    UIViewController *imageVC = [UIViewController new];
    CGRect screenRect = [[UIScreen mainScreen] bounds];
    CGFloat screenWidth = screenRect.size.width;
    CGFloat screenHeight = screenRect.size.height;
    imageVC.view.frame = CGRectMake(0, 0, screenWidth, screenHeight);
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:imageVC.view.frame];
    cell.vendorImage.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:fullImg]]];
    //    cell.imageView.image = fullImg;
    imageView.image = cell.vendorImage.image;
    imageView.userInteractionEnabled=YES;
    UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:imageView.hidden=YES];
    [imageView addGestureRecognizer:tapGR];
    [self presentViewController:imageVC animated:YES completion:nil];

}

提前致谢

2 个答案:

答案 0 :(得分:1)

您没有将图片添加到imageVC。您将呈现一个黑色的空视图控制器。

您应该创建UIViewController的子类。将NSImage属性添加到视图控制器。将图像视图添加到类中并在loadView(或在Interface Builder文件(xib或storyboard)中)初始化它。

- (void)loadView {
    UIView *contentView = [[UIView alloc] initWithFrame:...];

    self.imageView = [[UIImageView alloc] initWithFrame:...];

    [contentView addSubview:self.imageView];

    self.view = contentView;
}

将图像设置为viewWillAppear:中的图像视图。

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    self.imageView.image = self.image;
}

添加一个dismiss方法。

- (void)dismiss {
    [self dismissViewControllerAnimated:YES completion:nil];
}

然后像这样呈现视图控制器:

MyImageViewController *imageVC = [[MyImageViewController alloc] init];
cell.vendorImage.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL  URLWithString:fullImg]]];
imageVC.image = cell.vendorImage.image;
imageView.userInteractionEnabled=YES;
UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismiss)];
[imageView addGestureRecognizer:tapGR];
[self presentViewController:imageVC animated:YES completion:nil];

答案 1 :(得分:0)

无需创建新的UIViewController,您可以使用UIView并将此视图添加到表视图上方的当前视图中,并在其上添加图像视图。