将图像从 tableViewcell(Xib 文件)传递给另一个 ViewController
Posted
技术标签:
【中文标题】将图像从 tableViewcell(Xib 文件)传递给另一个 ViewController【英文标题】:Passing Image to another ViewController from a tableViewcell (Xib file) 【发布时间】:2019-05-13 16:03:14 【问题描述】:screenShot of the tableView cell with 3 images
我已使用 xib 文件生成 tableview 单元格。在 tableViewcell 类中,我正在传递 1/2/3/更多图像以显示像 facebook 这样的图像。现在,当我点击 tableviewcell 的 imageView(可以是 1/2/3)时,我希望它全屏显示图片。我在图像视图中添加了 tabguest 识别器。但我无法将图像传递给第二个 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
【问题讨论】:
不要使用点击手势。使用在您选择/点击单元格时调用的表委托方法 didSelectRow。 需要添加手势,因为我已经在使用 didselectRow 方法来查看 textField 的更多行为。 【参考方案1】:我认为您没有向 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
【讨论】:
以上是关于将图像从 tableViewcell(Xib 文件)传递给另一个 ViewController的主要内容,如果未能解决你的问题,请参考以下文章
在 xib 文件的 TableViewCell 中显示可变数量的图像的最佳方法是啥?
无法将 XIB 加载到可重用的 TableViewCell 中
Swift:来自 XIB 文件的多个自定义 TableViewCell
ios tableViewCell.xib文件生成多个单元格