2017-10-11 61 views
-2

我想在當我在一個UICollectionViewCell.I裏面的tableView中選擇一行時沒有在我的項目中使用storyboard的時候顯示一個視圖控制器。我嘗試在'didSelectRowAt indexPath:'內部調用'present(viewController,animated:true,completion:nil)',但是我得到一個錯誤'SecondPageCollectionViewCell'沒有成員'present',我也嘗試執行一個segue,但我仍然得到相同的錯誤。當我點擊一行時,如何顯示另一個視圖控制器?UICollectionViewCell'沒有會員'存在'

class SecondPageCollectionViewCell: UICollectionViewCell,UITableViewDelegate, UITableViewDataSource { 

let viewController = ViewController() 
let tableView = UITableView() 
... 

override func awakeFromNib() { 
... 
} 

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

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
     print("selected row \(indexPath.row)") 
} 

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 

    return allUsers.descriptions.count 
} 

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 
    return frame.height - secondViewheight 

} 

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    ... 
} 

} 
+0

ViewController能夠呈現,因此您還可以嘗試呈現在寡婦身上! –

+0

你既可以在故事板上使用segues,也可以編寫明確的_did-select-row-at-index-path_方法來呈現新的內容 - 字面上數百萬的例子都在網絡中。 – holex

回答

2

只有一個UIViewController可以提出另一個UIViewController。這樣做的最好方法是使用代理來告訴您的UIViewController提供新視圖