ios - 单击表格 View 单元格时转到另一个 View

标签 ios swift uitableview tableview

我知道以前有人问过这个问题,但我找到的所有解决方案对我来说真的没有意义。我是 iOS 的初学者,我知道如何左键单击并将按钮拖动到另一个 View 以进行转换,但我不能用 tableview 做到这一点。我的布局只有两个 View Controller ,一个带有 tableview,第二个是空白的,我最终想为每个 tableview 单元格点击更新标签。

下面的代码是第一个 View Controller ,包含使基本 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. 从您的 tableViewController 拖动到 SecondViewController 以创建一个 Segue。 (像这样)

enter image description here

根据需要选择类型。

  1. 现在选择 Segue 并在属性检查器中,为字段“Identifier”指定任意名称。我们稍后将使用它作为引用。

enter image description here

  1. 现在在您的 ViewVontroller.swift 文件中,导航到函数 didSelectRowAtIndexPath。在此您可以调用函数 performSegueWithIdentifier 并提供您之前提供的名称。这是代码:

enter image description here

希望这对您有所帮助。 :)

关于ios - 单击表格 View 单元格时转到另一个 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32128710/

相关文章:

ios - 使用 Swift 从 iOS 应用程序发布到 Facebook

swift - TableView Controller 和放置在 View Controller 中的 TableView 之间的区别

ios - 先执行代码! swift

ios - 使用 reuseIdentifier 调用 super.tableView(tableView : UITableView, cellForRowAtIndexPath...

ios - 使用特定标识符设置所有单元格的高度?

ios - Xcode iPhone应用程序运行并立即崩溃

iphone - 无法使用 MPMoviePlayerViewController 播放视频

ios - GStreamer 和 Swift

objective-c - 应用程序在绘制 UILabel 时崩溃 - NSString 是僵尸

ios - 从另一个 View Controller 快速修改一个变量