仍在迅速採取babysteps。Swift/iOs:如何鏈接動態添加的表格單元格?
對於靜態的tableview我只是CTRL +拖動表格單元向右導航控制器和選擇視圖顯示/場景。
但是,如果我使用了「動態原型」表視圖我如何能做到這一點?我像這樣填充表格視圖
let testMenu = [
(「menuItem 1"),
("menuItem test"),
(「afd"),
(「test"),
(「barca"),
(「london")
]
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return testMenu.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = UITableViewCell()
let (menuItem) = testMenu[indexPath.row]
cell.textLabel?.text = menuItem
return cell
}
這將按預期方式顯示菜單項。但是將每個表格單元鏈接到正確的導航控制器對我而言並不清楚。看一些教程,我想我需要使用didSelectRowAtIndexPath,但嘗試以下沒有打印任何內容,我不知道如何創建鏈接,segue?
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
//CODE TO BE RUN ON CELL TOUCH
//need to go to the right navigation controller
print("clicked: \(indexPath)")
}
和類
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
EDIT 1:我能夠打印 「被點擊:{長度= 2,路徑= 0 - 1}」。在連接檢查器中,我必須拖動網點:代理 - >到視圖控制器
你能提供一些虛擬代碼? – alex
我沒有太多熟悉的快捷,我是一個客觀的C語言開發,還是等待一段時間,我會揣摩...... –
你可以看到上面的代碼,如果你發現任何更多的困難,PLZ在這裏評論 –