仍在迅速采取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在这里评论 –