簡體   English   中英

通過單擊表視圖中的單元格 - Swift iOS打開新的視圖控制器

[英]Open new View Controller by clicking Cell in Table View - Swift iOS

我有以下onClick函數

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    tableView.deselectRowAtIndexPath(indexPath, animated: true)

    let row = indexPath.row
    println("Row: \(row)")

    println(meetingArray[row] as! String)

}

打印出單擊的單元格上的文本。 它工作正常。

我只是想知道如何設置一個函數來引導你到新的視圖控制器

編程方式:

let destination = UIViewController() // Your destination
navigationController?.pushViewController(destination, animated: true)

故事板:
首先,您必須為視圖設置標識符。 在下面的屏幕截圖中,您可以看到輸入標識符的位置。

截圖

之后,您可以使用以下代碼創建“目標”並將其推送到導航控制器:

let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
let destination = storyboard.instantiateViewController(withIdentifier: "YourViewController") as! YourViewController
navigationController?.pushViewController(destination, animated: true)

Segue公司:
首先,您必須為segue設置標識符,如下所示:

segue1

segue2

performSegue(withIdentifier: "segue", sender: self)

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "segue" {
        // Setup new view controller
    }
}

編輯:更新為Swift 3.x

在storyboard中,在Identity Inspector中設置viewController的storyboardId。

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    tableView.deselectRowAtIndexPath(indexPath, animated: true)

    let row = indexPath.row
    println("Row: \(row)")

    println(meetingArray[row] as! String)

    let secondViewController = self.storyboard.instantiateViewControllerWithIdentifier("storyBoardIdFor your new ViewController") as SecondViewController
    self.navigationController.pushViewController(secondViewController, animated: true)
}

對於遇到此問題的其他用戶,如果UITableViewController有靜態單元格,則只需控制從單元格拖動到新的View Controller。

在此輸入圖像描述

如果您不需要將任何數據傳遞到下一個View Controller,也可以在動態單元格上執行此操作。 但是,如果您確實需要傳遞數據,那么您應該從Table View的視圖控制器本身而不是單元格中創建segue 然后以編程方式調用segue。

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    self.performSegue(withIdentifier: "mySegueIdentifier", sender: self)
}

試試這個對我有用,

   func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    print("You tapped cell number \(indexPath.section).")
    print("Cell cliked value is \(indexPath.row)")

    if(indexPath.row == 0)
    {
    let storyboard = UIStoryboard(name: "Main", bundle: nil)

    let controller = storyboard.instantiateViewController(withIdentifier: "LoginViewController") as! LoginViewController

    self.navigationController?.pushViewController(controller, animated: true)

    }
  }

希望這對某些人有所幫助。

如果使用,

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)

所選行的值太晚傳遞給另一個視圖控制器。 您最好在func prepareForSegue中傳遞所選行,如下所示,首先聲明一個全局變量以將值傳遞給其他viewController

var globalVar:Int 

在基於故事板的應用程序中,您通常需要在導航之前做一些准備工作

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
        let selectedIndex = self.tableView.indexPathForCell(sender as! UITableViewCell)
        globalVar = selectedIndex!.row
        print("tableView prepareForSegue: " + String(globalVar))

    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM