通过快速单击TableView单元格转到ViewController [英] Go to the ViewController by clicking on the TableView cell in swift

查看:204
本文介绍了通过快速单击TableView单元格转到ViewController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个MenuViewController类,其中记录了表中记录的数组:

there is a class MenuViewController in which the array recorded in the table is recorded:

import Foundation
import UIKit

class MenuViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

@IBOutlet weak var menuTableView: UITableView!

let myTitle = ["Помощь", "Информация", "Поддержка"]


override func viewDidLoad() {
    super.viewDidLoad()


    menuTableView.delegate = self
    menuTableView.dataSource = self
}

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

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = menuTableView.dequeueReusableCell(withIdentifier: "MenuCell") as! MenuTableViewCell
    cell.labelText.text = myTitle[indexPath.row]
    return cell
}
}

您需要在其中写什么才能进入信息控制器?

What do you need to write in it to go to Info Controller?

推荐答案

我假设您正在使用情节提要.首先,您需要在两个视图控制器之间建立某种连接.您可以通过设置"segue"来做到这一点.您按住ctrl键并单击从第一个视图控制器到第二个视图控制器的拖动.看起来像这样:

I assume you are using storyboards. First you need to set up some sort of connection between your two viewcontrollers. You can do this by setting up a "segue". You hold ctrl+click drag from the first viewcontroller to the 2nd viewcontroller. It looks like this:

放开鼠标,您将看到以下内容:

When you let go of the mouse, you will see this:

单击显示".现在,您将创建一个segue. 现在您需要命名.因此,请单击情节提要中显示的segue.它基本上只是从菜单视图控制器到信息视图控制器的箭头.在右侧,您会看到一个可以命名的地方(给它一个标识符):

Click on Show. Now you will have created a segue. Now you need to name it. So click on the segue that shows up in the storyboard. It is basically just an arrow from the Menu View Controller to the Info View Controller. On the right hand side you will see a place where you can name it (give it an identifier):

现在...您已经完成了情节提要中需要做的所有事情.现在,在MenuViewController中的实际代码中,需要关联单击tableviewcell.您可以通过使用委托方法didSelectRowAt来做到这一点.

Now ... you have done all you needed to do in the storyboard. Now in your actual code in the MenuViewController, you need to associate clicking the tableviewcell. You do this by using the delegate method didSelectRowAt.

我看到您已经用以下行设置了委托: menuTableView.delegate = self

I see you have already set up the delegate with this line: menuTableView.delegate = self

这确保了didSelectRowAt会在用户点击行时被调用.

That ensures that didSelectRowAt will be called whenever the user taps on a row.

所以现在您要做的是编写代码来执行segue:

So now what you want to do is write the code to perform the segue:

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

注意: 这是有关与tableview行交互的更多信息: 如何检测被触摸或单击的tableView单元格迅速

Note: Here is more information on interacting with a tableview row: How to detect tableView cell touched or clicked in swift

以下是有关segues的更多信息: IOS-如何使用swift以编程方式进行搜索

Here is more information about segues: IOS - How to segue programmatically using swift

您还可以执行更多自定义操作,例如通过segues将数据传递到下一个Viewcontroller.这是如何做: 通过segue传递数据

There are many more customizations you can do ... such as passing data through to the next viewcontroller via the segues. Here's how: Pass data through segue

这篇关于通过快速单击TableView单元格转到ViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆