【问题标题】:Going to another view when I click a tableview cell单击表格视图单元格时转到另一个视图
【发布时间】:2015-08-20 21:29:02
【问题描述】:

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

下面的代码是第一个视图控制器,其中包含使基本 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()
    }
}

【问题讨论】:

  • 在 Xcode 中创建一个新的 Split View 项目来帮助您理解这件事的一个非常简单的事情。这将设置所有内容,以便点击单元格更改标签,并且应该让您很好地了解从那里去哪里。

标签: ios swift uitableview tableview


【解决方案1】:
  1. 从 tableViewController 拖到 SecondViewController 以创建 Segue。 (像这样)

根据您的要求选择类型。

  1. 现在选择 Segue 并在属性检查器中,为字段“标识符”指定任何名称。稍后我们将使用它作为参考。

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

希望这会有所帮助。 :)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多