首页 文章

使用CollectionViewCell中的按钮将数据从collectionview Cell解析为新的viewController

提问于
浏览
0

我有一个collectionView,我使用我在类中创建的数组填充 . collectionView可以工作,但问题是我想使用collectionViewCell中的按钮来转换到另一个viewController,以及将该单元格中的特定数据解析为viewController .

我想要做的一个例子是,如果我按下单元格中的按钮,我想要将问题图像转移到下一个viewController,我希望viewController的图像成为问题图像,也许我将来添加其他一些对应的数据 .

我已经查看了这个网站和其他网站上的其他答案,这让我走得很远,但是我找不到任何解决这个问题的问题 . 我知道它类似于tableView,但是当我尝试基于tableView更改它时,但它似乎对我不起作用 .

这是第一个和第二个视图的图像

这是我创建的类:

import Foundation
import UIKit

class Information {

var image = ""
var button = ""
var infoOne = ""
var infoTwo = ""
var price = ""

init(image: String, button: String, infoOne: String, infoTwo: String, price: String) {
    self.image = image
    self.button = button
    self.infoOne = infoOne
    self.infoTwo = infoTwo
    self.price = price
}

//MARK: Carousel Data

static func createData() -> [Information] {
    return [
        Information(image:  "question", button: "Buy One", infoOne: "First Data", infoTwo: "Second Data", price: "10"),
        Information(image: "exclamation", button: "Buy Two", infoOne: "First Data", infoTwo: "Second Data", price: "20"),
        Information(image: "period", button: "Buy Three", infoOne: "First Data", infoTwo: "Second Data", price: "30"),
        Information(image: "all", button: "Buy Four", infoOne: "First Data", infoTwo: "Second Data", price: "40")
    ]
   }
}

填充collectionViewCell时,此数组工作正常 .

这是collectionViewCell

import UIKit

class InfoCollectionViewCell: UICollectionViewCell {

var infomation: Information! {
    didSet {
        updateUI()
    }
}

var addBtnAction : (()-> ())?

@IBOutlet weak var infoImage: UIImageView!

@IBOutlet weak var infoLblOne: UILabel!

@IBOutlet weak var infoLblTwo: UILabel!

@IBOutlet weak var priceLbl: UILabel!

@IBOutlet weak var buyBtn: UIButton!

fileprivate func updateUI() {
    infoImage.image! = UIImage(named: infomation.image)!
    infoLblOne.text = infomation.infoOne
    infoLblTwo.text = infomation.infoTwo
    priceLbl.text = infomation.price
    buyBtn.setTitle(infomation.button, for: .normal)
}

@IBAction func buyBtnPressed(_ sender: Any) {
    print("INFORMATION: \(infomation.image)")
    addBtnAction?()
}

override func layoutSubviews() {
    super.layoutSubviews()
    self.layer.cornerRadius = 5.0
    self.clipsToBounds = true
  }
}

这是viewController:

import UIKit

class ViewController: UIViewController {

let collectionViewCellId: String = "carouselCollectionCell"
@IBOutlet weak var collectionView: UICollectionView!

fileprivate var information = Information.createData()

override func viewDidLoad() {
    super.viewDidLoad()
    self.collectionView.delegate = self
}

}

extension ViewController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {

    func numberOfSections(in collectionView: UICollectionView) -> Int {
    return 1
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return information.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "infoCell", for: indexPath) as! InfoCollectionViewCell
    cell.addBtnAction = {
        self.performSegue(withIdentifier: "mainSegue", sender: self)
    }
    cell.infomation = self.information[indexPath.item]
    return cell
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "mainSegue" {
        let nxtVC = segue.destination as? DetailViewController
        let cell = sender as! InfoCollectionViewCell
        let myIndexPath = self.collectionView.indexPath(for: cell)
        nxtVC?.infomation = information[(myIndexPath?.item)!]
    }
}
}

因此,当我运行应用程序时,一切都很好,直到我按下按钮来segue和解析数据 . 应用程序崩溃,这就是原因:

这是输出控制台中的内容:

无法将'carousel.ViewController'类型的值(0x107992178)转换为'carousel.InfoCollectionViewCell'(0x107991f98) . 2018-09-28 21:22:52.116698-0400 carousel [1573:38999]无法将'carousel.ViewController'类型的值(0x107992178)转换为'carousel.InfoCollectionViewCell'(0x107991f98) . (LLDB)

如果可能的话,我宁愿保留按钮,但如果不可能,我会改变它 . 我不确定我做错了什么 . 如果还有其他任何我可以提供的帮助,请告诉我 . 非常感谢你 .

编辑:

似乎我忘了包含一个重要的视图,一个是另一个被segued的viewController .

线程1:致命错误:在展开Optional值时意外发现nil

这是我认为的观点:

import UIKit

class DetailViewController: UIViewController {


@IBOutlet weak var infoImage: UIImageView!

@IBOutlet weak var dataTxtView: UITextView!

var testingImage: UIImage = UIImage()

var infomation: Information! {
    didSet {
        updateUI()

    }
}



override func viewDidLoad() {
    super.viewDidLoad()


}

fileprivate func updateUI() {


    let images = UIImage(named: infomation.image)
    infoImage.image = images

    print("Image: \(infomation.image)")

    }
}

在updateUI函数中,我试图使infoImage与infomation.image相同,但它崩溃并指出:

线程1:致命错误:在展开Optional值时意外发现nil

这对我来说没有多大意义,因为我试图打印infomation.image的字符串是什么,并且它有效,但是当我尝试将它作为图像放置时,它似乎崩溃了 . 再次感谢你

EDIT2:

我弄清楚我做错了什么 . 似乎我总是试图使用可选字符串 . 我如何修复它是因为我改变了我制作的类而不是字符串并将其转换为图像它已经是图像了 . 我还尝试创建一个if let语句来删除可选项 .

这就是prepareForSegue在主ViewController中的样子

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "mainSegue" {
        let nxtVC = segue.destination as? DetailViewController

        let cell = sender as? InfoCollectionViewCell
        let myIndexPath = self.collectionView.indexPath(for: cell!)

        if let indexPath = myIndexPath {

            nxtVC?.infomation = information[indexPath.row]

        }

    }
}

这就是viewDidLoad的样子:

override func viewDidLoad() {
    super.viewDidLoad()
    print("***********TESTINGIMAGE: \(infomation.image!)***********")

    infoImage.image = infomation.image!

}

我知道我正用一个展开图像!但我不知道如何删除可选项 . 感谢您的帮助 .

1 回答

  • 2

    问题是您假设 prepare 中的 sender 是单元格的实例,但您对 performSegue 的调用是将 self 作为发送方传递 . 但是 self 在这种情况下是视图控制器,而不是单元格 .

    解决方案是传递单元而不是 self .

    cell.addBtnAction = {
        self.performSegue(withIdentifier: "mainSegue", sender: cell)
    }
    

相关问题