0

私は、collectionViewCellのボタンがクリックされたときにViewControllerを表示しようとします。TableViewCellのcollectionViewのcollectionViewCellのボタンがクリックされたときに新しいViewControllerを表示します。

私はdynamic tableViewに、tableViewCellの1つにある動的なcollectionViewを持っています。他のビューコントローラ

を表示するためにあらゆるcollectionViewCellsで

、セグエのトリガーであるべきボタンがあり、私は次のコードが、アプリがクラッシュしてみました:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Receiver (<Okm.bienController: 0x7fc8b0cfb1a0>) has no segue with identifier 'extend'' 

私のstoryBoardのsegue識別子は 'extend'です。私は私のcollectionCellのボタンと機能clickExtendを()リンク

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
      self.segue() 
      // self.performSegueWithIdentifier("image", sender: nil) 

    } 

    func clickExtend(sender:Int){ 
     print(sender) //print the correct tag! 
     self.segue() 
    } 

    func segue(){ 
     self.performSegueWithIdentifier("image", sender: nil) 
    } 

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 

     if segue.identifier == "image" { 
      let backItem = UIBarButtonItem() 
      backItem.title = "" 
      navigationItem.backBarButtonItem = backItem 
      // print(sender) 

      let extendView = segue.destinationViewController as! extendController 
      extendView.image = UIImage(named: "maison") 

     } 
    } 
} 

enter image description here

class photoCell: UITableViewCell, UICollectionViewDelegateFlowLayout, UICollectionViewDelegate, UICollectionViewDataSource { 
    ... 

    func extendFunc(sender : UIButton) { 

     bienController().performSegueWithIdentifier("extend", sender: sender.tag) 
    } 
} 


class bienController : UIViewController, UITableViewDelegate, UITableViewDataSource { 

... 

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
     if segue.identifier == "extend" { 
      let backItem = UIBarButtonItem() 
      backItem.title = "" 
      navigationItem.backBarButtonItem = backItem 

      let extendView = segue.destinationViewController as! extendController 
      extendView.image = image[sender] 

     } 
    } 
} 

はその後、私はこれを試してみました。 segueはdidSelectRowAtIndexPathから来たときに機能しますが、ボタンをクリックすると同じエラーでクラッシュします: 'キャッチされていない例外' NSInvalidArgumentException 'のためアプリケーションを終了しています、理由:' Receiver() 「を私はセグエで実行せずにビューコントローラを表示するために直接試みる

」(私はセグエ識別子変更

を「画像」)。

class photoCategoryCell: UITableViewCell, UICollectionViewDelegateFlowLayout, UICollectionViewDelegate, UICollectionViewDataSource { 

... 
    func extendFunc(sender : UIButton) { 
     print(sender.tag) 
     bienController().boo(sender.tag) 
    } 
} 

そして:

class bienController : UIViewController, UITableViewDelegate, UITableViewDataSource { 

... 

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
       self.boo(indexPath.row) 

    } 

func boo(sender:Int){ 
     let storyboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil) 
     let vc:UIViewController = storyboard.instantiateViewControllerWithIdentifier("extend") as! extendController 
     let backItem = UIBarButtonItem() 
     backItem.title = "" 
     navigationItem.backBarButtonItem = backItem 

     self.showViewController(vc as UIViewController, sender: nil) 
    } 
} 

tableViewCellに私をクリックすると、それは、ビューコントローラを示しているが、私はcollectionViewCellで "拡張" をボタンをクリックすると、何もhappendsません。..

お願いします。私は助けが必要です!

ありがとうございました!最初のケースで

フルコード

import UIKit 

class photoCategoryCell: UITableViewCell, UICollectionViewDelegateFlowLayout, UICollectionViewDelegate, UICollectionViewDataSource { 

    @IBOutlet weak var collectionView : UICollectionView! 

    var hid = [true, true, true, true, true, true] //supp 

    var imageList = [UIImage]() 

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

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

     return 10 
    } 

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
     let cell = collectionView.dequeueReusableCellWithReuseIdentifier("photoCell", forIndexPath: indexPath) as! photoCollectionViewCell 

     cell.photoView.image = self.imageList[indexPath.row] 
     cell.photoView.contentMode = .ScaleAspectFill 
     cell.extend.hidden = true 
     cell.blurView.hidden = true 

     cell.extend.tag = indexPath.row 
     cell.extend.addTarget(self, action: #selector(photoCategoryCell.extendFunc(_:)), forControlEvents: .TouchUpInside) 
     return cell 
    } 

    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { 
     if let cell : photoCollectionViewCell = collectionView.cellForItemAtIndexPath(indexPath) as! photoCollectionViewCell! 
     { 
      if cell.blurView.hidden == true { 
      cell.blurView.hidden = false 
      cell.extend.hidden = false 
     } else { 
      cell.blurView.hidden = true 
      cell.extend.hidden = true 
     } 
     } 
    } 

    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize { 

     let cellWidth = (UIScreen.mainScreen().bounds.width)/2 
     return CGSize(width: cellWidth, height: cellWidth) 

    } 

    func extendFunc(sender : UIButton) { 
     print(sender.tag) 
     bienController().boo(sender.tag) 
    } 
} 

import UIKit 

class bienController : UIViewController, UITableViewDelegate, UITableViewDataSource { 

    @IBOutlet weak var tableView : UITableView! 
    @IBOutlet weak var tableViewTwo : UITableView! 
    @IBOutlet weak var tableHeight : NSLayoutConstraint! 
    @IBOutlet weak var viewBlack : UIView! 
    let onglet = ["One", "Two", "Three", "Four", "Five"] 


    var imageNb : Int = 3 


    override func viewDidLoad() { 
     super.viewDidLoad() 
//  let gradient: CAGradientLayer = CAGradientLayer() 
//  gradient.frame = self.viewBlack!.bounds 
//  gradient.colors = [UIColor.clearColor().CGColor, UIColor(red: 225/255, green: 225/255, blue: 225/255, alpha: 0.5).CGColor] 
//  
//  self.viewBlack.layer.insertSublayer(gradient, atIndex: 0) 

     self.tableView?.tableFooterView = UIView(frame: CGRectZero) 
     self.tableViewTwo?.tableFooterView = UIView(frame: CGRectZero) 
     self.tableHeight?.constant = 38*5 
     self.tableViewTwo?.scrollEnabled = false 

    } 

    override func viewWillAppear(animated: Bool) { 
     super.viewWillAppear(true) 
     UIApplication.sharedApplication().statusBarStyle = UIStatusBarStyle.Default 
     if let navigationBar = navigationController?.navigationBar { 
      navigationBar.barTintColor = UIColor.whiteColor() 
      navigationBar.translucent = false 
      if let font = UIFont(name: "HelveticaNeue-Light", size: 20.0) { 
       let navBarAttributesDictionary : [String : AnyObject]? = [ 
        NSForegroundColorAttributeName: UIColor(red: 235/255, green: 67/255, blue: 27/255, alpha: 1.0), 
        NSFontAttributeName: font 
       ] 
       navigationBar.titleTextAttributes = navBarAttributesDictionary 
       UIBarButtonItem.appearance().setTitleTextAttributes(navBarAttributesDictionary, forState: .Normal) 
      } 
     } 


     let editButton = UIBarButtonItem(title: "Modifier", style: .Plain, target: self, action: #selector(bienController.modifier)) 

     self.navigationItem.rightBarButtonItem = editButton 

    } 

    func modifier(){ 

     //do something 
    } 

    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { 
     return UITableViewAutomaticDimension 
    } 

    func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { 
     return UITableViewAutomaticDimension 
    } 

    func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
     return 1 
    } 

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     if tableView == self.tableView { 

      return 3 

     } else { 

      return 5 
     } 
    } 

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     if tableView == self.tableView { 

      if indexPath.row == 0 { 

       var cell = tableView.dequeueReusableCellWithIdentifier("oneCell") as! listCommandeCellTwo! 

       if cell == nil { 

        cell = listCommandeCellTwo(style: UITableViewCellStyle.Default, reuseIdentifier: "oneCell") 
       } 

       cell.descriptionLabel.text = "description" 
       cell.titre.text = "titre" 

       return cell 

      } else if indexPath.row == 1 && self.imageNb < 3 { 

       var cell = tableView.dequeueReusableCellWithIdentifier("bienImgCell") as! bienImgCell! 

       if cell == nil { 

        cell = bienImgCell(style: UITableViewCellStyle.Default, reuseIdentifier: "bienImgCell") 
       } 

       cell.imageOne.image = UIImage(named: "maison") 
       cell.imageTwo.image = UIImage(named: "maison") 
       cell.imageOne.contentMode = .ScaleAspectFill 
       cell.imageTwo.contentMode = .ScaleAspectFill 


       return cell 

      } else if indexPath.row == 1 && self.imageNb >= 3 { 

       var cell = tableView.dequeueReusableCellWithIdentifier("photoCategoryCell") as! photoCategoryCell! 

       if cell == nil { 

        cell = photoCategoryCell(style: UITableViewCellStyle.Default, reuseIdentifier: "photoCategoryCell") 
       } 

       cell.imageList.removeAll(keepCapacity: false) 
       for i in 0...10 { 
       cell.imageList.append(UIImage(named: "maison")!) 
       } 


       cell.collectionView.delegate = cell 
       cell.collectionView.dataSource = cell 

       return cell 

      } else { 
       var cell = tableView.dequeueReusableCellWithIdentifier("bienCellOne") as! bienCellOne! 

       if cell == nil { 

        cell = bienCellOne(style: UITableViewCellStyle.Default, reuseIdentifier: "bienCellOne") 
       } 


       cell.commentaire.delegate = cell 
       cell.commentaire.editable = false 
       cell.commentaire.setContentOffset(CGPointZero, animated: false) 

       cell.commentaire.text = "Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda." 

       return cell 
      } 
     } else { 


      var cell = tableView.dequeueReusableCellWithIdentifier("Cell") as! bienCellTwo! 

      if cell == nil { 

       cell = bienCellTwo(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell") 
      } 


      cell.titre.text = self.onglet[indexPath.row] 
      cell.imageCategory.image = UIImage(named: "validate") 
      cell.imageCategory.contentMode = .ScaleAspectFit 

      if indexPath.row == 0 { 
       cell.backgroundColor = UIColor(red: 235/255, green: 67/255, blue: 27/255, alpha: 1.0) 
       cell.tintColor = UIColor.whiteColor() 
       cell.titre.textColor = UIColor.whiteColor() 
       let checkImage = UIImage(named: "chevron") 
       let checkmark = UIImageView(image: checkImage) 
       cell.accessoryView = checkmark 
       cell.addBottomBorderWithColor(UIColor(red: 235/255, green: 67/255, blue: 27/255, alpha: 1.0), width: 1.0) 
      } else { 
       cell.addBottomBorderWithColor(UIColor(red: 225/255, green: 225/255, blue: 225/255, alpha: 1.0), width: 1.0) 
       cell.backgroundColor = UIColor.whiteColor() 
       cell.titre.textColor = UIColor(red: 235/255, green: 67/255, blue: 27/255, alpha: 1.0) 
       let checkImage = UIImage(named: "chevron-orange") 
       let checkmark = UIImageView(image: checkImage) 
       cell.accessoryView = checkmark 

      } 
      return cell 
     } 

    } 

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
     //if tableView == self.tableViewTwo { 

      print(indexPath.row) 
      self.boo(indexPath.row) 
      // self.segue() 
      // self.performSegueWithIdentifier("image", sender: nil) 
     //} 
    } 

    func boo(sender:Int){ 
     print(sender) 
     let storyboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil) 
     let vc:UIViewController = storyboard.instantiateViewControllerWithIdentifier("extend") as! extendController 
     let backItem = UIBarButtonItem() 
     backItem.title = "" 
     navigationItem.backBarButtonItem = backItem 

     self.showViewController(vc as UIViewController, sender: nil) 

     //self.segue() 
    } 

    func segue(){ 
     // self.performSegueWithIdentifier("image", sender: nil) 
    } 

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 

     if segue.identifier == "image" { 
      let backItem = UIBarButtonItem() 
      backItem.title = "" 
      navigationItem.backBarButtonItem = backItem 

      let extendView = segue.destinationViewController as! extendController 
      extendView.image = UIImage(named: "maison") 

     } 
    } 
} 

答えて

2

、問題は、この行である:

bienController().performSegueWithIdentifier("extend", sender: sender.tag) 

は標準init方法を用いbienControllerクラスの新しいインスタンスを作成します。その結果、ストーリーボードの知識がなくなり、結果的にセグを見つけることができなくなります。

2番目のケースは、ピンダウンするのが難しいです(clickExtendメソッドがcollectionViewCellのボタンからどのように呼び出されているかはわかりませんが)、同じ問題があると思われます。

最後に、bienControllerの新しいインスタンスを作成します。showViewControllerを使用しているため、セグの欠如には問題ありません。エラーはありませんが、新しいインスタンスが画面に表示されないため、showViewControllerは何もしません(少なくとも画面には何も表示されません)。

bienViewController既存のインスタンスへの参照がコレクションビューのセルに必要です。私は正確にこれを行う方法を示すためにあなたの設定の詳細を参照する必要があります一方で、あなたはthis questionと答えをチェックする必要があります。

この場合、の既存のbienControllerビューコントローラでメソッドを呼び出すには、photoCategoryCellが必要です。

protocol PhotoCategoryCellDelegateProtocol { 
    func didTapExtendInPhotoCollectionViewCellWithIndex(index: Int, inPhotoCategoryCell: photoCategoryCell) 
} 

を次に、このタイプのphotoCategoryCelldelegateプロパティを追加します:photoCategoryCellでは、それが呼び出すことを期待する方法を示しているプロトコル定義置き換える、

var delegate : PhotoCategoryCellDelegateProtocol? 

そしてextendFunc方法でのbienController().boo(sender.tag)

self.delegate?.didTapExtendInPhotoCollectionViewCellWithIndex(sender.tag, inPhotoCategoryCell: self) 

bienControllerクラスでは、この新しい機能:自己にphotoCategoryCelldelegateプロパティを設定してください、cellForRowAtIndexPath

class bienController : UIViewController, UITableViewDelegate, UITableViewDataSource, PhotoCategoryCellDelegateProtocol { ... 

func didTapExtendInPhotoCollectionViewCellWithIndex(index: Int, inPhotoCategoryCell: photoCategoryCell) { 
    self.boo(index) // or use performSegue if you prefer 
} 

が、それはこのプロトコルを採用していることを示すためにbienControllerのクラス定義を改正

 ... 
     } else if indexPath.row == 1 && self.imageNb >= 3 { 

      var cell = tableView.dequeueReusableCellWithIdentifier("photoCategoryCell") as! photoCategoryCell! 

      if cell == nil { 

       cell = photoCategoryCell(style: UITableViewCellStyle.Default, reuseIdentifier: "photoCategoryCell") 
      } 

      cell.imageList.removeAll(keepCapacity: false) 
      for i in 0...10 { 
      cell.imageList.append(UIImage(named: "maison")!) 
      } 
      // set the cell's delegate... 
      cell.delegate = self 
      cell.collectionView.delegate = cell 
      cell.collectionView.dataSource = cell 

      return cell 
     } else ... 

私は、コレクションビューセルインデックステーブルビューセル:コレクションビューの複数のテーブルビュー行がある場合は、正しいTV行を決定できます。

最終的なスタイルノートとして、クラス名が大文字で始まることが確立されています。私は今、その習慣に入るので、bienControllerからBienControllerなどを修正してください。

+0

ありがとう@pbasdf!私は何かこれが間違っていると思った:bienController()。performSegueWithIdentifier( "extend"、sender:sender.tag)、私は正しかった..しかし、実際に別のView Controllerから関数を正しく呼び出す方法がわからない。私は私のフルコードを追加しました、どのように私はbiogControllerの関数を呼び出すことができますphotoCategoryCellからSegueを実行するには?大いに感謝する!私はこのバグが修正されることを望む! –

+0

@ L.Valentin私は自分の答えを更新しました。 – pbasdf

+0

こんにちは@pbasdf!私は十分にあなたに感謝できません!それは素晴らしいレッスンでした!ありがとう、今すぐ代議員は私のためにもっと意味がある!クラス名のアドバイスをありがとう! –

関連する問題