传递数据表视图 - >视图控制器

时间:2016-11-20 13:38:18

标签: ios swift uitableview firebase swift3

我有这个tableview,我想将用户名传递给视图控制器并在标签中显示,但它不起作用。用户名来自firebase数据库。我还检查了其他问题,但不能将其用于我的问题。我不是编码专家:

import Foundation
import UIKit
import Firebase
import FirebaseDatabase
import FirebaseAuth

struct user {
     let username : String!
}

class MainScreen: UITableViewController {

     var Users = [user]()

     override func viewDidLoad() {

     let databaseRef = FIRDatabase.database().reference()

     databaseRef.child("users").queryOrderedByKey()
                .observe(.childAdded, with: { snapshot in

        print(snapshot)
        let username = (snapshot.value as? NSDictionary)!["username"] as! String
        print(username)
        self.Users.append(user(username: username))
        self.tableView.reloadData()
        print(self.Users)
    })
}

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

override func tableView(_ tableView: UITableView, 
                        cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell")
    let nameLbl = cell?.viewWithTag(1) as! UILabel
    nameLbl.text = self.Users[indexPath.row].username
    return cell!
    }

@IBAction func ShowDetail(_ sender: Any) {
    var selectedLabel:String?

    func tableView(tableView: UITableView, 
                    didSelectRowAtIndexPath indexPath: NSIndexPath) {
        print("Row \(indexPath.row)selected")
        selectedLabel = self.Users[indexPath.row].username
        performSegue(withIdentifier: "detailView", sender: self)
    }

    func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

        if(segue.identifier == "detailView") {
            let vc = segue.destination as! CallDetail
            vc.stringPassed = selectedLabel!
        }
     }
}

接收的ViewController看起来像这样:

import UIKit

class CallDetail: UIViewController {

     @IBOutlet weak var UserName: UILabel!
     var stringPassed = String()

     override func viewDidLoad() {
         super.viewDidLoad()

         UserName = stringPassed.text
     }
}

2 个答案:

答案 0 :(得分:0)

didSelectRowAtIndexPathprepareForSegue方法都应该在@IBAction func ShowDetail(_ sender: Any)之外(实际上,我无法确定其目的):

selectedLabel声明为实例变量,并将方法分开:

import Foundation
import UIKit
import Firebase
import FirebaseDatabase
import FirebaseAuth

struct user {
    let username : String!
}

class MainScreen: UITableViewController {

    // 'selectedLabel' should be here:
    var selectedLabel:String?

    var Users = [user]()

    override func viewDidLoad() {

        let databaseRef = FIRDatabase.database().reference()

        databaseRef.child("users").queryOrderedByKey().observe(.childAdded,    with: {
            snapshot in

            print(snapshot)

            let username        = (snapshot.value as? NSDictionary)!["username"] as! String

            print(username)

            self.Users.append(user(username: username))

            self.tableView.reloadData()

            print(self.Users)

        })
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return Users.count
    }
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell")

        let nameLbl = cell?.viewWithTag(1) as! UILabel

        nameLbl.text = self.Users[indexPath.row].username

        return cell!

    }

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        print("Row \(indexPath.row)selected")
        selectedLabel = self.Users[indexPath.row].username
        performSegue(withIdentifier: "detailView", sender: self)
    }

    func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if(segue.identifier == "detailView") {
            let vc = segue.destination as! CallDetail

            vc.stringPassed = selectedLabel!
        }
    }

    // just remove it if you don't need it...
    @IBAction func ShowDetail(_ sender: Any) {

    }

}

另外,请确保tableView数据源和委托已连接到viewController。

答案 1 :(得分:0)

首先你需要删除

tableView(tableView: UITableView, didSelectRowAtIndexPath因为当你单击一个单元格时会自动调用它,如果你已经将tableView委托设置为viewController。

prepareForSegue将在performSegue(withIdentifier: "detailView", sender: self)之后调用,并删除ShowDetail(_ sender: Any)这不是必需的。

并更改这些行

let nameLbl = cell?.viewWithTag(1) as! UILabel

nameLbl.text = self.Users[indexPath.row].username

cell.textLabel?.text = self.Users[indexPath.row].username