我是 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/