2017-01-02 1 views
0

私はこれを可能な限り明確にするつもりです。私のXcodeプロジェクトでは、プロトタイプのセルを再利用しています。それぞれのセルに別のView Controllerを開くようにします。あなたはどうやってそれをすることができると思いますか?ここでテーブルのセルは大きいです

は私のMain.storyboardで再利用可能なセルの写真です:ここで

This is the pic of the reusable cell

は再利用されている細胞の結果である:

This is the link to pic 2

そしてここにはあります私のコントローラクラスのコード:

import UIKit 

class NewsTableViewController: UITableViewController { 

    @IBOutlet var menuButton:UIBarButtonItem! 
    @IBOutlet var extraButton:UIBarButtonItem! 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     tableView.estimatedRowHeight = 242.0 
     tableView.rowHeight = UITableViewAutomaticDimension 

     if revealViewController() != nil { 
      menuButton.target = revealViewController() 
      menuButton.action = #selector(SWRevealViewController.revealToggle(_:)) 

      revealViewController().rightViewRevealWidth = 150 
      extraButton.target = revealViewController() 
      extraButton.action = #selector(SWRevealViewController.rightRevealToggle(_:)) 

      view.addGestureRecognizer(self.revealViewController().panGestureRecognizer()) 
     } 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

    // MARK: - Table view data source 

    override func numberOfSections(in tableView: UITableView) -> Int { 
     // Return the number of sections 
     return 1 
    } 

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     // Return the number of rows 
     return 3 
    } 

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! NewsTableViewCell 

     // Configure the cell... 
     if indexPath.row == 0 { 
      cell.postImageView.image = UIImage(named: "watchkit-intro") 
      cell.postTitleLabel.text = "WatchKit Introduction: Building a Simple Guess Game" 
     } else if indexPath.row == 1 { 
      cell.postImageView.image = UIImage(named: "custom-segue-featured-1024") 
      cell.postTitleLabel.text = "Building a Chat App in Swift Using Multipeer Connectivity Framework" 
     } else { 
      cell.postImageView.image = UIImage(named: "webkit-featured") 
      cell.postTitleLabel.text = "A Beginner’s Guide to Animated Custom Segues in iOS 8" 
     } 

     return cell 
    } 

} 

明確にするために、pic 2の各セルに別のView Controllerを開かせたいと思います。たとえば、セル1で新しいビューコントローラ1を開き、セル2で新しいビューコントローラ2を開き、セル3で新しいビューコントローラ3を開きます。

ありがとう!

UPDATE

私は君たちの一つが示唆されたが、今、私はこれ持って、次のコードを使用しています、セル1をタップしたので、もし私は、3つの新しいクラスを作っ

Xcode gave me an error

をセル1のビューコントローラが開き、セル2がタップされている場合は、セル2のビューコントローラが開きます。

明らかにXcodeは私にエラーを与えています。 提案がありますか?ここで

は、読書のための私のMain.storyboard Note that I have entered a class and id for all the new 3 view controllers

おかげで、私はあなたが私に解決策を与えることができることを望みます!

+2

_「表のセルは大きい」_それはどういう意味ですか? –

+0

私が持っている質問は、なぜ各セルが別のビューコントローラを開くようにしたいのですか?それは何かのようなものです、あなたはx種類のビューコントローラを持っていて、いくつかのセルはあるタイプをトリガし、他のタイプは他のタイプをトリガしますか?または、あなたのテーブルビュー内のすべてのセルを別のビューコントローラで開くように計画していますか?これは私に再び尋ねるよう促します:なぜですか? – WERUreo

+0

@WERUreoうん!あなたは正しいことを推測します。彼は、異なるセルをクリックするごとに異なるVCを開くことを望んでいました。しかし、私は彼のUITableViewに50個の細胞があるなら、彼は50個のVCを必要とし、それは良い習慣ではありませんでした。 –

答えて

1

は、私はあなたが、例えばテーブルビューのデリゲートメソッド

を実施すべきだと思います

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) 
{ 
    if indexPath.row == 0 
    { 
      // open VC1 
    } 
    else if indexPath.row == 1 
    { 
     // open VC2 
    } 
    else if indexPath.row == 2 
    { 
     // open VC3 
    } 
} 
0

あなたはtableView(_:, didSelectRowAt:)を実装する必要があります:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
    if indexPath.row == 0 { 
     let newViewController1 = ... 
     present(newViewController1, animated: true, completion: nil) 

    } else if indexPath.row == 1 { 
     let newViewController2 = ... 
     present(newViewController2, animated: true, completion: nil) 

    } else { 
     let newViewController3 = ... 
     present(newViewController3, animated: true, completion: nil) 
    } 
} 

また、あなたのテーブルビューセルにジェスチャー認識を追加して、カスタムセルデリゲートを実装することができます。あなたの状況でそれが必要なようには思われないので、私はそれをお勧めしません。それは残忍かもしれないようです。あなたが興味を持っている場合は、しかし:エラーの両方について

class NewsTableViewCell: UITableViewCell { 
    var delegate: NewsTableViewCellDelegate? 

    override func awakeFromNib() { 
     let recognizer = UIGestureRecognizer(target: self, action: #selector(tapped)) 
     contentView.addGestureRecognizer(recognizer) 
    } 

    func tapped() { 
     delegate?.cellTapped(self) 
    } 
} 

protocol NewsTableViewCellDelegate { 
    func cellTapped(_ cell: NewsTableViewCell) 
} 

extension MyViewController: NewsTableViewCellDelegate { 
    func cellTapped(_ cell: NewsTableViewCell) { 
     if cell == cell1 { } 
     else if cell == cell2 { } 
     else if cell == cell3 { } 

     //You would have to manually determine how to check if a cell is cell1, cell2, or cell3, since you wouldn't have an indexPath available. 
    } 
} 
0

あなたがあなたのViewConrollerを提示storyboardからではなく、コードの作業でセグエを使用しているため、

  1. main.storyboardからセグエを削除します。コードワークで

  2. 、あなたは、コードの下にこれを使用し、あなたのVCを推進している場合は、以下の

**

`let storyboard = UIStoryboard(name: "main.Storyboard", bundle: nil) 
let controller = storyboard.instantiateViewController(withIdentifier:"myViewController") as! UIViewController 
self.present(controller, animated: true, completion: nil)` 

**

して行を置き換える:

let myViewController = self.storyboard?.instantiateViewController(withIdentifier: "myViewController") as! Conversation_VC 
self.navigationController?.pushViewController(myViewController, animated: true) 
+0

Xcodeはあなたのコードがうまくいかず、私はセグを取り除いたことを伝えています。 Xcodeがそれを伝えています – appmaker

+0

@appmakerどのラインで、私にスクリーンショットを見せてください –

+0

@appmakerあなたのGmailのiDを教えてください。 –

1

iこのようなものだと思う。

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
    switch indexPath.row { 
    case 0: 
     // segue to view 1 
    case 1: 
     // segue to view 2 
    case 2: 
     // segue to view 3 
    case 3: 
     // segue to view 4 
    default: 
     // default go here 
    } 
} 
関連する問題