2017-01-02 136 views
0

我打算儘可能使這一點變得清晰。在我的Xcode項目中,我重複使用了一個原型單元,並且希望每個單元打開一個不同的視圖控制器。你們如何認爲我能做到這一點?表格單元格很大

這裏是我的Main.storyboard可重複使用的電池的PIC:

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 
    } 

} 

爲了清楚起見,我希望圖片2中的每個單元打開一個不同的視圖控制器。例如,我希望Cell 1打開New View Controller 1,Cell 2打開New View Controller 2,Cell 3打開New View Controller 3。

感謝您的閱讀!

UPDATE

我用下面的代碼你們的一個建議,但現在我有這個:

Xcode gave me an error

我做了3個新的類別,因此,如果小區1被竊聽,它會打開單元格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個不同類型的視圖控制器,一些單元格會觸發一種類型,而其他單元格會觸發其他類型?或者你打算讓tableview中的每個單元格打開一個不同的視圖控制器?這促使我再次問:爲什麼? – WERUreo

+0

@WERUreo是啊!你猜對了。他想打開不同的VC,點擊每個不同的單元格。但我沒有得到,如果他的「UITableView」中有50個單元格,那麼他需要50個VC,這不是一個好習慣。 –

回答

1

我想你應該實現的tableview的委託方法

例如

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) 
    } 
} 

或者,你可以一個手勢識別器添加到您的表視圖細胞和實現自定義單元格的委託。雖然這似乎不是你需要的,所以我不會推薦它。看起來這可能是矯枉過正。因爲你使用賽格瑞從storyboard但在代碼的工作,你介紹你的ViewConroller

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

對於這兩種錯誤,

  1. 刪除賽格瑞從main.storyboard:如果你有興趣,雖然。

  2. 在代碼中工作,通過以下取代你行

**

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

**

如果你推你的VC,然後用這個下面的代碼:

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

Xcode告訴我你的代碼不起作用,並且我已經移除了這些segues。 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 
    } 
}