菜鸟教程小白 发表于 2022-12-11 16:50:38

ios - UITableView Segue 查看 Controller 不工作


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

<pre><code>@IBOutlet weak var tableView: UITableView!

var friends = [&#34;A&#34;,&#34;B&#34;,&#34;C&#34;,&#34;D&#34;]

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) -&gt; Int {
    return self.friends.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -&gt; UITableViewCell {
    let cell = UITableViewCell()
    let friend = self.friends
    cell.textLabel!.text = friend
    return cell

}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    self.performSegueWithIdentifier(&#34;detailSegue&#34;, sender: nil)
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    let listVC = segue.destinationViewController as! FriendDetailViewController
}
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>您应该在 tableView 委托(delegate)中分配给 ViewController </p>

<pre><code>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

}
</code></pre>

<p>当然还要加上这个:</p>

<pre><code>class ViewController : ... , UITableViewDelegate
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - UITableView Segue 查看 Controller 不工作,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/38034946/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/38034946/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - UITableView Segue 查看 Controller 不工作