gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 05:46:25 24 4
gpt4 key购买 nike

screenShot of the tableView cell with 3 images

我使用了 xib 文件来生成 tableview cell。在 tableViewcell 类中,我传递 1/2/3/更多图像来显示像 facebook 一样的图像。现在,当我点击 tableviewcell 的 imageView(可以是 1/2/3)时,我希望它全屏显示图片。我在 ImageView 中添加了 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

}
}

最佳答案

我认为你没有向第二个VC传递任何信息

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

关于ios - 将图像从 tableViewcell 传递到另一个 ViewController(Xib 文件),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56116045/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com