したがって、UiCollectionViewを作成しています。コレクションビューで画像の配列をスクロールすることができます。画像の1つをタップすると、別のビューコントローラに移動し、タップした画像がUICollectionViewから表示されます。私はチュートリアルを見て一歩一歩進みましたが、私が間違っていることは分かりません。コレクションビューを表示してスクロールすることはできますが、画像の1つをタップすると、別のビューコントローラーには移動しません。私はクラスと識別子をチェックしました。私はそれらがすべて正しいと思う。 UICollectionView Swift 3から独立したView Controllerで画像を表示する
collectionView
は出口collectionViewCell
識別子“Cell”
collectionViewCell
がクラスがOurCell
viewController
ある
出口であると呼ばれるcollectionViewCell
にcollectionView
UIimage
と呼ばれるその意志イメージを保持する
@IBOutlet weak var collectionView: UICollectionView!
let imageArray = [UIImage(named: "EliHouse1"), UIImage(named: "EliHouse2"), UIImage(named: "EliHouse3"), UIImage(named: "iPod")]
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.imageArray.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! OurCell
cell.ourImage?.image = self.imageArray[indexPath.row]
return cell
}
func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
{
if segue.identifier == "showImage"
{
let indexPaths = self.collectionView!.indexPathsForSelectedItems!
let indexPath = indexPaths[0] as NSIndexPath
let vc = segue.destination as! SecondViewController
vc.image = self.imageArray[indexPath.row]!
}
}
ここでここでOurCell
クラスファイルのコード
@IBOutlet weak var ourImage: UIImageView!
がSecondViewController
クラスのコードです:eはここViewController
クラスファイルのコードがされsecondViewController
と呼ばれていますファイル
@IBOutlet weak var imageView: UIImageView!
var image = UIImage()
override func viewDidLoad() {
super.viewDidLoad()
self.imageView.image = self.image
// Do any additional setup after loading the view.
}
コード内で実行して、セルを表示してスクロールすることはできますが、画像をタップすると、SecondViewController
には移動せず、何も起こりません。誰にもアイデアはありますか?私は間違って何をしていますか?あなたのcollectionViewクラスで
あなたは 'showImage'セグの識別子をどこで定義しますか?あなたのストーリーボードも表示できますか? –
まず、 'IndexPath'インスタンスを' NSIndexPath'に変換する必要はありません。 'IndexPath'を直接使うことができます。' storyboard'でも 'UICollectionViewCell'から' SecondViewController'にsegueを作成しましたか? –