OStack程序员社区-中国程序员成长平台

标题: ios - UITableView Segue 查看 Controller 不工作 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 16:50
标题: ios - UITableView Segue 查看 Controller 不工作

在我的应用程序中,我有一个表格 View ,其中列出了 friend 的姓名,当您单击表格 View 中的姓名时,它应该执行转入另一个存储此 friend 信息的 View Controller 的操作。虽然我可以点击这些行,但它并没有在 friend 信息 View Controller 中执行 segue。有想法该怎么解决这个吗?提前致谢!

@IBOutlet weak var tableView: UITableView!

var friends = ["A","B","C","D"]

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

self.tableView.dataSource = self
self.tableView.dataSource = self

}

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

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = UITableViewCell()
    let friend = self.friends[indexPath.row]
    cell.textLabel!.text = friend
    return cell

}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    self.performSegueWithIdentifier("detailSegue", sender: nil)
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    let listVC = segue.destinationViewController as! FriendDetailViewController
}



Best Answer-推荐答案


您应该在 tableView 委托(delegate)中分配给 View Controller

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after 

    loading the view, typically from a nib.

    self.tableView.dataSource = self
    self.tableView.delegate = self

}

当然还要加上这个:

class ViewController : ... , UITableViewDelegate

关于ios - UITableView Segue 查看 Controller 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38034946/






欢迎光临 OStack程序员社区-中国程序员成长平台 (http://ostack.cn/) Powered by Discuz! X3.4