将图像从tableViewcell传递到另一个ViewController(Xib文件)

时间:2019-05-13 16:03:15

标签: ios swift uitableview

screenShot of the tableView cell with 3 images

我使用了xib文件来生成tableview cell。在tableViewcell类中,我传递了1/2/3 /更多图像以显示诸如facebook之类的图像。现在,当我点击tableviewcell的imageView时(可能是1/2/3),我希望它以全屏显示图片。我在图像Views中添加了tapguest识别器。但是我无法将图像传递给第二个ViewController。有什么帮助吗?

cellForRowAt索引路径代码:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "mycell", for: indexPath) as! MyCell
        cell.delegate = self
        cell.index =  indexPath.row

        cell.cellConfig(withText: text , withImage: (photo3 as? [UIImage]))
        cell.isExpanded = false

        let tapToNavigate =  UITapGestureRecognizer(target: self, action: #selector(handleDetailsPicture))
        let tapToNavigate2 = UITapGestureRecognizer(target: self, action: #selector(handleDetailsPicture))
        let tapToNavigate3 = UITapGestureRecognizer(target: self, action: #selector(handleDetailsPicture))


        tapToNavigate.numberOfTapsRequired = 1

        cell.photoView1.isUserInteractionEnabled = true
        cell.photoView2.isUserInteractionEnabled = true
        cell.photoView3.isUserInteractionEnabled = true

        cell.photoView1.addGestureRecognizer(tapToNavigate)
        cell.photoView2.addGestureRecognizer(tapToNavigate2)
        cell.photoView3.addGestureRecognizer(tapToNavigate3)


        return cell
    }

tapguest识别器处理程序代码:

    @objc func handleDetailsPicture() {

        let secondViewController = self.storyboard!.instantiateViewController(withIdentifier: "DetailsPictureViewController") as! DetailsPictureViewController
        self.navigationController!.pushViewController(secondViewController, animated: true)

   }

单元格类功能代码:

func cellConfig(withText text:String? , withImage Image: [UIImage]?) {


        self.seeMoreInPicture.isHidden = true

        photoView1.contentMode = .scaleAspectFill
        photoView2.contentMode = .scaleAspectFill
        photoView3.contentMode = .scaleAspectFill

        photoView1.clipsToBounds = true
        photoView2.clipsToBounds = true
        photoView3.clipsToBounds = true


        if text == nil {
            self.textViewHeightCOnstrain.constant = 0
            self.bottomSpaceBetSeemoreAndContainer.constant = 0
            self.imageContainerViewHeight.constant = contentView.frame.height

        }

        if text != nil && isExpanded == false {

            let width = UIScreen.main.bounds.size.width

            if text!.heightWithConstrainedWidth(width: width, font:  UIFont.systemFont(ofSize: 14)) > 100 {
                self.textViewHeightCOnstrain.constant = 100
                self.seeMoreButton.isHidden = false
            }

            else if text!.heightWithConstrainedWidth(width: width, font:  UIFont.systemFont(ofSize: 14)) < 100 {
                self.textViewHeightCOnstrain.constant = text!.heightWithConstrainedWidth(width: width, font:  UIFont.systemFont(ofSize: 14))
                self.seeMoreButton.isHidden = true
            }
            self.textView.text = text
        }


       self.textView.text = text

        if Image == nil {
            let width = UIScreen.main.bounds.size.width
            self.textViewHeightCOnstrain.constant = (text?.heightWithConstrainedWidth(width: width, font: UIFont.systemFont(ofSize: 14)))!
            self.imageContainerViewHeight.constant = 0
            self.photoView2Height.constant = 0
            self.photoView3Height.constant = 0
            self.bottomSpaceBetSeemoreAndContainer.constant = 0
        }


        else if Image?.count == 1 {
            self.imageContainerView1Width.constant = UIScreen.main.bounds.size.width
            self.trailingBetweenContainers.constant = 0
            self.photoView1.image = Image![0]

        }

        else if Image?.count == 2 {
            self.photoView3Height.constant = 0
            self.photoView2Height.constant = imageContainerViewHeight.constant
            self.photoView1.image = Image![0]
            self.photoView2.image = Image![1]

        }

        else if Image?.count == 3 {
            self.photoView1.image = Image![0]
            self.photoView2.image = Image![1]
            self.photoView3.image = Image![2]

        }

        else {
            self.photoView1.image = Image![0]
            self.photoView2.image = Image![1]
            self.photoView3.image = Image![2]

            self.seeMoreInPicture.isHidden = false

        }
    }

secondViewCOntroller代码:

class DetailsPictureViewController: UIViewController {

    @IBOutlet weak var detailsImage: UIImageView!

    var imageToShow = UIImage()

    override func viewDidLoad() {
        super.viewDidLoad()

       self.detailsImage.image = imageToShow

    }
}

1 个答案:

答案 0 :(得分:0)

我认为您没有将任何信息传递给secondVC

class DetailsPictureViewController: UIViewController {

    @IBOutlet weak var detailsImage: UIImageView!

    var imageToShow = UIImage()

    override func viewDidLoad() {
        super.viewDidLoad()

       self.detailsImage.image = imageToShow

    }
}

此代码未设置或告诉应用程序要做的任何事情

self.detailsImage.image = imageToShow

告诉应用准备prepareForSegue并设置图像时该怎么做

 override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

        if segue.identifier == "DetailsPictureViewController" {
            let destination = segue.destinationViewController as! DetailsPictureViewController
            destination.imageToShow = photoView1.image //example
        }
    }

然后在您的 secondViewCOntroller

@IBOutlet weak var detailsImage: UIImageView!
var imageToShow : UIImage?

在viewDidLoad

postingImage.image = imageToShow