【问题标题】:Swift: How to display data from View Controller to Details View ControllerSwift:如何将数据从 View Controller 显示到 Details View Controller
【发布时间】:2016-09-30 09:28:01
【问题描述】:

我想将特定数据从 TableView 显示到 Details_View_Controller。

我有两个标签。一个标签是标题,我在 Details(Conference)_View_Controller 中将其显示为标题。

但我有更多详细信息要显示在 Details_V_C 中。我从 Firebase 数据库中检索到的所有数据。在控制台中检索所有内容,但如何在 Details_View_Controller 中显示所有内容?

我读过这篇文章:https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/TableView_iPhone/TableViewAndDataModel/TableViewAndDataModel.html#//apple_ref/doc/uid/TP40007451-CH5-SW2,但我有一个 SWIFT 3,这篇文章没有正确解释最新版本的 swift。

JSON 树:

"conferences": {

              "date": "some date"
              "deadline": "some deadline"
              "place": "some place"
              "title": "some title"

               }

视图控制器(TableView):

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

        let cell = tableView.dequeueReusableCellWithIdentifier("Cell")

        let messageSnapshot: FIRDataSnapshot! = conf[indexPath.row]
        let message = messageSnapshot.value as! Dictionary<String, String>

       // print(message)

        let label1 = cell?.viewWithTag(1) as! UILabel
        label1.text = message["title"]

        let label2 = cell?.viewWithTag(2) as! UILabel
        label2.text = message["place"]


        return cell!

    }

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {



        let messageSnapshot: FIRDataSnapshot! = conf[indexPath.row]
        let message = messageSnapshot.value as! Dictionary<String, String>

        let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
        let confDetails = storyBoard.instantiateViewControllerWithIdentifier("conferenceDetails") as! ConferenceViewController
        confDetails.title = message["title"]



        confDetails.conferenceDetailDict = message
        self.navigationController?.pushViewController(confDetails, animated: true)

    }

详细信息(会议)视图控制器:

class ConferenceViewController: UIViewController {

    var conferenceDetailDict: Dictionary<String, String> = Dictionary()

    override func viewDidLoad() {
        super.viewDidLoad()

        print(self.conferenceDetailDict)
        // Do any additional setup after loading the view.

    }

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

有人可以帮我解决这种情况吗?

【问题讨论】:

  • 我认为你走对了,print(self.conferenceDetailDict) 的输出是什么?
  • 在设置“title”属性时,ConferenceViewController 的视图尚未加载。在 ConferenceViewController 中为标题创建一个字符串变量,例如 var titleString: String?然后在 viewDidLoad() 上分配该标题,例如 self.title = self.titleString
  • @Mr.UB 输出是来自这个数据库的所有内容,正如您在 JSON 树上看到的那样。一切都在输出中。
  • @jovanjovanovic 标题设置为会议视图控制器顶部的主标题。我使用这个内部函数来设置 ViewController 的标题。但我不知道如何设置在这个视图控制器中显示其余数据。

标签: ios swift uitableview firebase firebase-realtime-database


【解决方案1】:

到目前为止,我从您的问题中了解到,您无法从字典中检索所有数据

您可以通过以下方式访问字典的所有键和值

var keys = [String](conferenceDetailDict.keys)
var values = [String](conferenceDetailDict.values)

你可以像这样遍历你的字典

for (keys, values) in conferenceDetailDict {
    print("\(keys): \(values)")
}

在 viewdidload 中设置数据

【讨论】:

    【解决方案2】:

    一种方法是,如果您在 TableView 上,则将要在 Detail_V_C 访问的任何数据保存到 plist 中,并在加载 Detail_V_C 时获取

    请看这个链接:

    How can i save, retrieve, delete & update my data in Plist file in ios?

    let confDetails = storyBoard.instantiateViewControllerWithIdentifier("conferenceDetails") as! ConferenceViewController
            confDetails.title = message["title"]
    
            //Here use the code to save into plist
    
            confDetails.conferenceDetailDict = message
            self.navigationController?.pushViewController(confDetails, animated: true)
    

    当您加载 Details_V_C 访问 plist 并使用数据时,如果不再需要,则在关闭 confDetails_V_C 时删除所有数据。

    【讨论】:

    • 在这个解决方案中我应该创建另一个按钮来保存这些数据?或者当我点击tableView中的某个单元格时,会显示Detail_V_C中的自动保存到plistand?当我单击 tableView 的“后退按钮”时,这个 plist 会延迟吗?因为它可能会占用这部手机的一些内存。
    • 感谢您的回复,但我在“confDetails”中有所有值。我有:“confDetails.title = message[”place”] 并且我在 Dedails_V_C 标题中作为“place”值。但我想从这个 Dictionary 中检索其余变量并将其传递给 UILabel,以查看所有这些数据。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-15
    • 1970-01-01
    • 2019-11-10
    • 2020-02-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多