我以编程方式在一个TableViewControllers中创建了2个ViewController,如下所示:
// contents for labels in cells for tableviewcontrollers
let contents1 : [String] = ["One:","Two:","Three:","Four:","Five:"]
let contents2 : [String] = ["Six:","Seven:","Eight:","Nine:","Ten:"]
override func viewDidLoad() {
    super.viewDidLoad()
    table1.delegate = self
    table1.dataSource = self
    table2.delegate = self
    table2.dataSource = self   
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if(tableView.tag == 1)
    {
        return contents1.count
    }
    else if (tableView.tag == 2)
    {
        return contents2.count
}
    return 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  let cell = tableView.dequeueReusableCell(withIdentifier: "cell",  for: indexPath as IndexPath )
    if (tableView.tag == 1)
    {
        cell.textLabel?.text = contents1[indexPath.row]
    }
    else if (tableView.tag == 2)
    {
        cell.textLabel?.text = contents2[indexPath.row]
    }
return cell
}我的问题是,如果选择在不使用Segue的情况下显示下一个新的TableViewController,如何以编程方式链接第一个ViewController的标签“4:”?
发布于 2016-12-19 10:44:39
您可以使用UITableView's deSelectRowAt委托方法来标识单元格选择,然后再转到下一个视图。
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let cell = tableView.cellForRow(at: indexPath)
    if cell?.textLabel?.text == "Four" { // you can use you indexPath to compare as well
        let secondViewController = self.storyboard.instantiateViewControllerWithIdentifier("SecondViewController") as SecondViewController
        self.navigationController.pushViewController(secondViewController, animated: true)
    }
}发布于 2017-10-24 07:20:36
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
    cell.textLabel?.text = pets[indexPath.row]
    // Configure the cell...
    return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    myIndex = indexPath.row
    performSegue(withIdentifier: "segue", sender: self)https://stackoverflow.com/questions/41220059
复制相似问题