简体   繁体   English

单击表格视图单元格时转到另一个视图

[英]Going to another view when I click a tableview cell

I know this question has been asked before but all the solutions I have found have really not made sense to me. 我知道这个问题以前曾被问过,但是我发现的所有解决方案对我来说实际上都是没有意义的。 I am a beginner at iOS and I know how I can left click and drag a button to another view to make a transition, but I can not do it with the tableview. 我是iOS的初学者,我知道如何左键单击并将按钮拖动到另一个视图以进行过渡,但是使用tableview无法做到这一点。 My layout is just two view controllers, one with a tableview and the second is blank, and I would eventually want to update labels for each tableview cell click. 我的布局只有两个视图控制器,一个具有表格视图,第二个为空白,我最终希望为每次单击表格单元格更新标签。

The code below is for the first view controller with everything to make a basic tableview work 下面的代码适用于第一个视图控制器,它具有使基本tableview正常运行的所有功能

import UIKit

var fullList = [String]()
var dateList = [String]()

class ViewController: UIViewController, UITableViewDelegate {

    @IBOutlet var betTable: UITableView!

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

        if(NSUserDefaults.standardUserDefaults().objectForKey("fullList") != nil)   //check if the to do list exists
        {
            fullList = NSUserDefaults.standardUserDefaults().objectForKey("fullList") as! [String]      //store toDoList array into NSUserDefaults to save to iphone
        }

        if(NSUserDefaults.standardUserDefaults().objectForKey("dateList") != nil)
        {
            dateList = NSUserDefaults.standardUserDefaults().objectForKey("dateList") as! [String]
        }
    }

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

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
    {
        let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "cell")
        cell.textLabel?.text = fullList[indexPath.row]
        return cell
    }

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

    func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath)
    {
        if(editingStyle == UITableViewCellEditingStyle.Delete)
        {
            fullList.removeAtIndex(indexPath.row)
            NSUserDefaults.standardUserDefaults().setObject(fullList, forKey: "fullList")
            NSUserDefaults.standardUserDefaults().setObject(dateList, forKey: "dateList")

            betTable.reloadData()
        }
    }

    override func viewDidAppear(animated: Bool)
    {
        betTable.reloadData()
    }
}
  1. Drag from your tableViewController to the SecondViewController to create a Segue. 从tableViewController拖动到SecondViewController来创建Segue。 (like This) (像这样)

在此处输入图片说明

Select the type according to your requirement. 根据需要选择类型。

  1. Now select the Segue and in the attributes inspector, give any name to the field 'Identifier'. 现在选择Segue,然后在属性检查器中为“标识符”字段指定任何名称。 We will use this later for referencing purposes. 稍后我们将使用它作参考。

在此处输入图片说明

  1. Now in your ViewVontroller.swift file, navigate to function didSelectRowAtIndexPath . 现在在您的ViewVontroller.swift文件中,导航到函数didSelectRowAtIndexPath Inside this you can call the func performSegueWithIdentifier and provide the name you gave earlier. 在其中,您可以调用func performSegueWithIdentifier并提供您之前指定的名称。 Here is the code: 这是代码:

在此处输入图片说明

Hope this helps. 希望这可以帮助。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 单击TableView单元格时,表视图崩溃 - Table view crashes when I click on TableView cell 当我单击第一个表格视图的表格视图单元格时进入第二个表格视图? - Going into a second table view when i click on a table view cell of a first table view? 当我单击leftViewcontroller单元格时,MMDrawerController导航到另一个视图控制器 - MMDrawerController navigating to another view controller when I click on the leftViewcontroller cell 单击另一个视图控制器中的按钮时实现原型单元 - implement prototype cell when I click a button in another view Controller 当我在tableview中添加单元格数组时查看最后一个单元格 - View last cell when i add array of cell in tableview 当我单击没有元素的Swift时,Tableview单元格只会显示 - Tableview cell only segues when I click where there are no elements Swift 当我单击表格视图单元格时,UITextfield没有显示文本吗? - UITextfield didnt shown text when i click on tableview cell? 旋转iOS屏幕时,收藏夹视图单元出现问题 - When I rotate iOS screen, collection view cell is going wrong 如何在按钮单击同一单元格内时隐藏/显示自定义tableview单元格内的特定视图 - how to hide/show a specific view inside the custom tableview cell when button click inside the same cell 当我单击它时,UISearchBar会转到视图的顶部 - UISearchBar going to the view's top when I click on it
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM