将数据从UICollectionView传递到Swift 3中带有segue的新ViewController

时间:2017-08-10 22:23:10

标签: swift uitableview uicollectionview segue

我正在尝试从CollectionView单元格中选择时将数据传递给新的ViewController。我使用switch方法在HomeViewController上有2个集合视图。一切都显示正确,当我加载应用程序时,集合视图中的图像显示。问题是,当我选择应用程序中断的图像时。有趣的是,并非总是会发生。有时会工作,有时候会有效。这是模拟器。当我连接我的实际iPhone工作正常而不打破。此外,我传递数据的视图控制器具有包含节和行的表视图。这是我的代码:

class HomeViewController: UIViewController, SWRevealViewControllerDelegate,    UIScrollViewDelegate, UICollectionViewDataSource, UICollectionViewDelegate {

@IBOutlet weak var hoodsCollectionView: UICollectionView!
@IBOutlet weak var cuisineCollectionView: UICollectionView!

var hoodsArray = ["marina", "embac-1", "mission-1", "nbeach-1", "nob-1", "rhill-1", "ZHood-1"]
var cuisinesArray = ["marina", "embac-1", "mission-1", "nbeach-1", "nob-1", "rhill-1", "ZHood-1"] 

override func viewDidLoad() {
    super.viewDidLoad()


    hoodsCollectionView.dataSource = self
    cuisineCollectionView.dataSource = self

    hoodsCollectionView.delegate = self
    cuisineCollectionView.delegate = self
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

    switch collectionView {

    case self.hoodsCollectionView:
        return self.hoodsArray.count

    case self.cuisineCollectionView:
        return self.cuisinesArray.count

    default:
        return 0
    }
}


 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    switch collectionView {


    case self.hoodsCollectionView:

        let myCell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellHoods", for: indexPath) as! RestaurnatCollectionViewCell

        myCell.layer.cornerRadius = 4
        myCell.layer.borderColor = UIColor(red: 166/255, green: 166/255, blue: 166/255, alpha: 1.0).cgColor
        myCell.layer.borderWidth = 0.7

        myCell.myImage.image = UIImage(named: hoodsArray[indexPath.row])


        return myCell

    case self.cuisineCollectionView:

        let myCell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellCuisine", for: indexPath) as! RestaurnatCollectionViewCell

        myCell.myImage.image = UIImage(named: cuisinesArray[indexPath.row])

        return myCell

    default:
        return UICollectionViewCell()
    }
}



   func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)  {


    switch collectionView {

    case self.hoodsCollectionView:

        if let myCell = hoodsCollectionView.cellForItem(at: indexPath) {


        performSegue(withIdentifier: "cellHoods", sender: myCell)


    }

    case self.cuisineCollectionView:

    if let myCell = cuisineCollectionView.cellForItem(at: indexPath) {

        performSegue(withIdentifier: "cuisineHoods", sender: myCell)


        }

    default:
        break
    }


}



override func prepare(for segue: UIStoryboardSegue, sender: Any?) {


 if let indexPath = hoodsCollectionView.indexPath(for: sender as! RestaurnatCollectionViewCell) {

        if segue.identifier == "cellHoods" {

          let vc: HoodsViewController = segue.destination as! HoodsViewController

            vc.hoodsTest = hoodsArray[indexPath.row]


            }

    }

    if let indexPath = cuisineCollectionView.indexPath(for: sender as! RestaurnatCollectionViewCell) {

        if segue.identifier == "cuisineHoods" {

            let vc: CuisinesViewController = segue.destination as! CuisinesViewController

            vc.cuisineTest = cuisinesArray[indexPath.row]
        }
    }

  }

}

我正在使用segue.identifier来传递数据。我的HomeViewController在Storyboard中连接到HoodsViewController和CuisinesViewController。 在HoodsViewController中,我有一个var hoodsTest = String()和在SimilarViewController中相同:var cuisineTest = String() 当我选择图像时,传递数据并使用print(hoodTest)打印图片的名称。 我也在使用RestaurnatCollectionViewCell:

@IBOutlet var myImage: UIImageView!

当应用程序中断并给我错误时,我会转到HoodsViewController。 如果选择,也适用于CuisinesViewController。

Error snapshot Error snapshot2

cell.hoodLabel.text = self.filteredRestaurants[indexPath.section].restaurants[indexPath.row].name 

我将感谢任何建议和想法。我是Xcode的新手并且正在努力学习。 正如我所说,疯狂的部分是有时候工作,而其他人不在模拟器中。连接手机时始终有效! 再次感谢您的帮助和理解!

0 个答案:

没有答案