Swift 3:将TableViewController中的String传递给另一个TableViewController

时间:2016-12-29 15:52:56

标签: ios swift uitableview firebase

我有一个UITableViewController在列表中显示来自Firebase的所有用户。如果您点击一个用户,您会看到另一个UITableViewController,其中包含在Interface Builder中准备的静态TableView布局,以编辑用户属性。我想将所选用户的UID传递给DetailTableViewController,以便在那里加载所有当前用户数据。

编辑:此问题并非完全重复。我想将数据从一个UITableViewController传递到另一个UITableViewController而不是正常的详细信息UIViewController

这是我第一个TableViewController的当前代码。

有人能帮助我吗?我不明白。

UserListTableViewController.swift:

import UIKit
import Firebase
import FirebaseAuth
import FirebaseDatabase

class UserListTableViewController: UITableViewController {

    var dbRef:FIRDatabaseReference!
    var user = [User]()

    var writeSelectedUID:String!
    var selectedUID: String = "Mister X"

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        self.startObservingDB()
    }

    func startObservingDB () {
        dbRef.observe(.value, with: { (snapshot:FIRDataSnapshot) in
            var newUser = [User]()

            for user in snapshot.children {
                let userObject = User(snapshot: user as! FIRDataSnapshot)
                newUser.append(userObject)
            }
            self.user = newUser
            self.tableView.reloadData()
        }) { (error:Error) in
            print(error.localizedDescription)
        }
    }

    // MARK: - Table view data source
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return user.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "User Cell", for: indexPath) as! CustomUserListTableViewCell

        // Configure the cell
        let userRow = user[indexPath.row]
        cell.userFirstLastNameLabel?.text = "\(userRow.firstName!) \(userRow.lastName!)"
        cell.userUsernameLabel?.text = "@\(userRow.username!)"
        }
        return cell   
    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        //let selectedUID = user[indexPath.row]
        let selectedUserRow = user[indexPath.row]
        self.writeSelectedUID = "\(selectedUserRow)"
        performSegue(withIdentifier: "editUser", sender: self)
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        let viewcontroller = segue.destination as! ManageUserSettingsTableViewController
        // Pass the selected object to the new view controller.

        if(segue.identifier == "editUser") {
            viewcontroller.usernameTextField.text! = "\(self.writeSelectedUID)"
            print("Var: \(self.writeSelectedUID)")
        }
    }
}

1 个答案:

答案 0 :(得分:0)

在UserListTableViewController.swift中:

var detailItem: AnyObject?

usernameTextField.text = detailItem?.description

在你的AnotherViewController中:

class ClientASTMAdmin(admin.ModelAdmin):
    actions = ('exclude_keyword', )

    list_display = (
        'id',
        'day',
        'search_term',
        'is_exclude',
    )

    def exclude_keyword(self, request, queryset):
        for instance in queryset:
            instance.is_exclude = not instance.is_exclude
            instance.save()
    exclude_keyword.short_description = 'Exclude (or include) keywords'