ios - 链接单元格以调用不同的 ViewController

标签 ios swift tableview

我是 Swift 新手(我使用 2.0),没有任何编程经验。所以,我是新人)尝试做一些相当简单的事情。链接 UITableViewCell 单击以调用不同的 ViewController。这就像用户单击单元格汽车并打开新的ViewController

我已经创建了ViewController( StoryboardA、B、C、D、E)。 我怎样才能连接?如果我正确理解,我应该使用 didSelectRowAtIndexPath

我所做的代码:

import UIKit

class FirstViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {


let list = ["Cars", "Fruits", "Foods", "Toys"]
let listdesc = ["Toyota", "Apple", "Meat", "Fallout 4"]
let identities = ["A", "B", "C", "D"]

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return list.count;
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cellIdentifier: String = "cell"

    let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: cellIdentifier)

    cell.textLabel?.text = list[indexPath.row]
    cell.detailTextLabel?.text = listdesc[indexPath.row]

    return cell

}

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

    }

最佳答案

你应该在 didSelectRowForIndexPath 中这样做:

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

       let StoryBoard  = UIStoryboard(name: "Main", bundle: nil)

      if(list[indexPath.row] == "Cars") {
         var vcA = STORYBOARD.instantiateViewControllerWithIdentifier("A") as! A
         self.navigationController?.pushViewController(vcA, animated: true)
      } else if(list[indexPath.row] == "Fruits") {
          var vcB = STORYBOARD.instantiateViewControllerWithIdentifier("B") as! B
          self.navigationController?.pushViewController(vcB, animated: true)
      }

  }

以及您将按照我的方式管理的其他条件......

关于ios - 链接单元格以调用不同的 ViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40838109/

相关文章:

ios - 根据 iOS (Swift 2) 中的消息 ID 将传入的网络消息分派(dispatch)到特定线程

JavaFX 使用 OneToMany 关系填充 tableview

swift - 将数组数据加载到 TableView 中

iphone - iOS/iPhone 上的正常运行时间卡住

android - Android、iOS 和黑莓操作系统之间的主要区别是什么?

swift - 是否可以在 SceneKit 中计算时间轴 `Time` 变量的正弦值?

ios - addSubview 如何使用内部尺寸?

ios - Swift:每次出现 tableview 时如何重新加载新内容(例如数组)?

ios - iOS 设备中的 3D 广告

ios - 在不损失质量的情况下,用它的内容放大 UITextView 的图层