轻按表格单元格时如何推送新的视图控制器? [英] How to push a new view controller when a table cell is tapped?

查看:17
本文介绍了轻按表格单元格时如何推送新的视图控制器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了这个包含 3 个部分和 7 行的表格.代码如下所示

导入 UIKit类视图控制器:UIViewController、UITableViewDelegate、UITableViewDataSource {@IBOutlet var subjectTabelView:UITableView!var slSubject = ["English Lang&Lit", "Chinese Lang& Lit", "Economics"]var hlSubject = [数学"、化学"、生物学"]var tokSubject = ["知识论"]覆盖 func viewDidLoad() {super.viewDidLoad()//在加载视图后做任何额外的设置,通常是从笔尖.subjectTabelView.dataSource = selfsubjectTabelView.delegate = self}覆盖 func didReceiveMemoryWarning() {super.didReceiveMemoryWarning()//处理任何可以重新创建的资源.}func numberOfSectionsInTableView(tableView: UITableView) ->整数{返回 3}func tableView(tableView: UITableView, numberOfRowsInSection 部分: Int) ->整数{如果部分== 0{返回 hlSubject.count}else if 部分 == 1{返回 slSubject.count}别的 {返回 tokSubject.count}}func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) ->UITableViewCell {let subjectCell = tableView.dequeueReusableCellWithIdentifier("idSubjectCell", forIndexPath: indexPath) as!UITableViewCell如果 indexPath.section == 0 {subjectCell.textLabel?.text = hlSubject[indexPath.row]} else if indexPath.section == 1{subjectCell.textLabel?.text = slSubject[indexPath.row]} 别的 {subjectCell.textLabel?.text = tokSubject[indexPath.row]}返回主题单元格}func tableView(tableView: UITableView, titleForHeaderInSection 部分: Int) ->细绳?{如果部分 == 0 {返回HL"} else if section == 1{返回SL"} 别的 {返回ToK"}}}

我该怎么做才能让这个表格中的每个单元格在被点击时推送一个新的视图控制器?我的故事板的图片如下所示.在我的故事板,我的视图控制器中,我已经创建了一个导航控制器,并将包含表的视图控制器设为 rootViewController.现在,我的 tableView 只有一个原型单元格和一个单元格标识符.

谢谢!

解决方案

假设您的locationVC"是:

class LocationVC: UIViewController {@IBOutlet 弱变量 fromWhereLabel: UILabel!//这可以在创建这个 UIViewController 时改变var textToShow : 字符串?覆盖 func viewWillAppear(动画:布尔){如果让 textToShow = textToShow {fromWhereLabel.text = textToShow}}}

然后,只需将下面的函数添加到名为 UIViewControllerViewController 代码中(应该有一个更好的名称 ;-))你可以实现你的目标.

 func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {//如果这样的单元格存在并且目标控制器(要显示的那个)也存在..如果让subjectCell = tableView.cellForRowAtIndexPath(indexPath),让destinationViewController = navigationController?.storyboard?.instantiateViewControllerWithIdentifier("locationVC") as?位置VC{//这是一个奖励,我将在目标控制器上显示来自它的单元格的相同文本...if let text = subjectCell.textLabel?.text {destinationViewController.textToShow = 文本} 别的 {destinationViewController.textToShow = "Tapped Cell 的 textLabel 为空"}//然后只需将控制器推入视图层次结构中导航控制器? .pushViewController(destinationViewController,动画:真)}}

您将能够在每次点击单元格时启动一个 LocationVC UIViewController,并且它具有一些价值来证明它是正确的.:)

希望有帮助!

<块引用>

更新:以下代码和说明用于允许启动点击单元格

后不同的UIViewController

1.- 让我们创建一个类,作为我们每个新 UIViewController 的父类(我们愿意从 tableview 单元格的水龙头中取出的那些):

public class CommonDataViewController: UIViewController {//这里我们将放置我们想要与此视图共享的任何数据var 数据:AnyObject?}

2.- 让我们创建某种导航规则,只是为了进行组织 ;-)

enum Navigation: Int {情况 vc1 = 0, vc2 = 1, vc3 = 2, vc4 = 3//我们有多少规则(为了不超过这个数字)静态让定义导航 = 4//这必须返回故事板上此视图的标识符func storyboardIdentifier() ->细绳 {//为了这个例子,我们为每个新的视图控制器都有一个共同的前缀,如果不是这样,你可以在这里使用 switch(self)返回locationVC_\(self.rawValue + 1)"}}

现在,让我们以之前的代码为基础:

3.- 为清楚起见,让我们对之前的 LocationVC 稍作改动(在本例中,将有一个 故事板标识符,文本为 locationVC_1")

class LocationVC: CommonDataViewController {@IBOutlet 弱变量 fromWhereLabel: UILabel!//这是可选的,但提高了清晰度..这里我们拿我们的AnyObject?可变数据并将其转换为此视图除外的数据类型var thisVCReceivedData:字符串?{返回数据为?细绳}覆盖 func viewWillAppear(动画:布尔){如果让 textToShow = thisVCReceivedData {fromWhereLabel.text = textToShow}}}

4.- 现在,我们在 didSelectRowAtIndexPath 函数中触发所有这些.

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {//只是为了避免点击没有关联 UIViewController 的单元格如果 Navigation.definedNavigations >indexPath.row {//点击此单元格后我们最常访问的导航枚举上的视图实例让 nextView = Navigation(rawValue: indexPath.row)!//我们的Storyboard中目标CommonDataViewController的儿子的标识符让标识符 = nextView.storyboardIdentifier()//如果一切都存在...如果让subjectCell = tableView.cellForRowAtIndexPath(indexPath),让destinationViewController = navigationController?.storyboard?.instantiateViewControllerWithIdentifier(identifier) as?CommonDataViewController {//在这里你可以使用nextView"的开关来将不同的数据传递给每个视图控制器..对于这个例子,我们只是将相同的字符串传递给每个人if let text = subjectCell.textLabel?.text {destinationViewController.data = 文本} 别的 {destinationViewController.data = "Tapped Cell 的 textLabel 为空"}导航控制器? .pushViewController(destinationViewController,动画:真)}}}

<块引用>

请注意,您可以使用协议和委托方法获得相同的结果,这只是解释起来更简单

I have created this table with 3 sections and 7 rows. The code is shown below

import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

@IBOutlet var subjectTabelView: UITableView!

var slSubject = ["English Lang&Lit", "Chinese Lang&Lit", "Economics"]
var hlSubject = ["Mathematics", "Chemistry", "Biology"]
var tokSubject = ["Theory of Knowledge"]

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    subjectTabelView.dataSource = self
    subjectTabelView.delegate = self
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 3
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if section == 0{
        return hlSubject.count
    }else if section == 1{
        return slSubject.count
    }else {
        return tokSubject.count
    }

}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let subjectCell = tableView.dequeueReusableCellWithIdentifier("idSubjectCell", forIndexPath: indexPath) as! UITableViewCell

    if indexPath.section == 0 {
        subjectCell.textLabel?.text = hlSubject[indexPath.row]
    } else if indexPath.section == 1{
        subjectCell.textLabel?.text = slSubject[indexPath.row]
    } else {
        subjectCell.textLabel?.text = tokSubject[indexPath.row]
    }

    return subjectCell
}

func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    if section == 0 {
        return "HL"
    } else if section == 1{
        return "SL"
    } else {
        return "ToK"
    }
}



}

What do I have to do to make every cell in this table pushes a new view controller when it is tapped? The picture of my storyboard is shown below. In my storyboard, my view controller, I have already created a navigation controller, and made the view controller that has the table the rootViewController. And for now, my tableView has only one prototype cell and one cell identifier.

Thank you!

解决方案

Suppose your "locationVC" is:

class LocationVC: UIViewController {

    @IBOutlet weak var fromWhereLabel: UILabel!
    //This can be changed when creating this UIViewController
    var textToShow : String?

    override func viewWillAppear(animated: Bool) {
        if let textToShow = textToShow {
            fromWhereLabel.text = textToShow
        }
    }
}

then, just adding function below to your code in ViewController named UIViewController (that should have a better name ;-)) you can achieve your goal.

 func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
      //if such cell exists and destination controller (the one to show) exists too..
        if let subjectCell = tableView.cellForRowAtIndexPath(indexPath), let destinationViewController = navigationController?.storyboard?.instantiateViewControllerWithIdentifier("locationVC") as? LocationVC{
            //This is a bonus, I will be showing at destionation controller the same text of the cell from where it comes...
            if let text = subjectCell.textLabel?.text {
                destinationViewController.textToShow = text
            } else {
                destinationViewController.textToShow = "Tapped Cell's textLabel is empty"
            }
          //Then just push the controller into the view hierarchy
          navigationController?.pushViewController(destinationViewController, animated: true)
        }
     }

You will be able to have a LocationVC UIViewController launched every time you tap a cell, and it will have some value to prove it right. :)

Hope it Helps!

UPDATE: Code and Instructions below are for allowing to launch different UIViewControllers after tap on cells

1.- Let's create a class that will be the parent for every one of our new UIViewControllers (the ones we are willing to go from our tableview cell's tap):

public class CommonDataViewController: UIViewController {
//Here we are going to be putting any data we want to share with this view
  var data: AnyObject?
}

2.- Let's create some sort of Navigation rules, just to be organised ;-)

enum Navigation: Int {
    case vc1 = 0, vc2 = 1, vc3 = 2, vc4 = 3
    //How many rules we have (for not to exceed this number)
    static let definedNavigations = 4
    //This must return the identifier for this view on the Storyboard
    func storyboardIdentifier() -> String {
        //for this example's sake, we have a common prefix for every new view controller, if it's not the case, you can use a switch(self) here
        return "locationVC_\(self.rawValue + 1)"
    }
}

Now, let's build upon previous code:

3.- For clarity, let's change a little our previous LocationVC (that for this example, will have an Storyboard Identifier with the text "locationVC_1")

class LocationVC: CommonDataViewController {

    @IBOutlet weak var fromWhereLabel: UILabel!

    //This is optional, but improves clarity..here we take our AnyObject? variable data and transforms it into the type of data this view is excepting 
    var thisVCReceivedData: String? {
        return data as? String
    }

    override func viewWillAppear(animated: Bool) {
        if let textToShow = thisVCReceivedData {
            fromWhereLabel.text = textToShow
        }
    }
}

4.- Now, we trigger all of this in our didSelectRowAtIndexPath function.

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    //Just to avoid tapping on a cell that doesn't have an UIViewController asociated
    if Navigation.definedNavigations > indexPath.row {
        //The view's instance on our Navigation enum to which we most go after tapping this cell
        let nextView = Navigation(rawValue: indexPath.row)!
        //The identifier of the destination CommonDataViewController's son in our Storyboard
        let identifier = nextView.storyboardIdentifier()
        //If everything exists...
        if let subjectCell = tableView.cellForRowAtIndexPath(indexPath), let destinationViewController = navigationController?.storyboard?.instantiateViewControllerWithIdentifier(identifier) as? CommonDataViewController {

            //here you can use a switch around "nextView" for passing different data to every View Controller..for this example, we just pass same String to everyone
            if let text = subjectCell.textLabel?.text {
                destinationViewController.data = text
            } else {
                destinationViewController.data = "Tapped Cell's textLabel is empty"
            }
            navigationController?.pushViewController(destinationViewController, animated: true)
        }
    }
}

Notice that you can achieve same results using protocols and delegate approach, this is just simpler to explain

这篇关于轻按表格单元格时如何推送新的视图控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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