【问题标题】:segue to detailed Controller from Table View从表视图转到详细的控制器
【发布时间】:2017-07-11 14:25:48
【问题描述】:

我有一个 TableViewController,其中包含显示登录用户和应用程序中任何其他用户之间的名称、图片和最后一条消息的单元格。因为附加了一个单元格以仅显示未登录用户的名称。该单元格有登录用户的名称和没有登录用户的名称,因此为了显示未登录用户的名称,我使用了此代码。

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

if message.ReceiverId != self.loggedInUserUid {
            var newVariable = message.ReceiverId

        if let imageName =  newVariable {

            let ref = FIRDatabase.database().reference().child("users").child(imageName)

            ref.observeSingleEvent(of: .value, with: { (snapshot)
                in

                if let dictionary = snapshot.value as? [String: AnyObject]{
                    for post in dictionary {
                        let messages = snapshot.value as! [String: AnyObject]
                        //let messages = post.value as! [String: AnyObject]
                        for (id, value) in messages {

                            self.username = messages["username"] as? String

 }}}})}}else if message.senderId != self.loggedInUserUid {
et newVariable = message.senderId

            if let imageName =  newVariable {

                let ref = FIRDatabase.database().reference().child("users").child(imageName)

                ref.observeSingleEvent(of: .value, with: { (snapshot)
                    in

                    if let dictionary = snapshot.value as? [String: AnyObject]{
                        for post in dictionary {
                            let messages = snapshot.value as! [String: AnyObject]
                       //     let messages = post.value as! [String: AnyObject] ---> very confused why it changed from post to snapshot, if I put post.value, it doesn't let me run
                            for (id, value) in messages {


                                self.username = messages["username"] as? String

它会显示未登录的用户名,这是我想要的,但是当我转到新页面时,我还想传递该名称,具体取决于所选的单元格。

override public func prepare(for segue: UIStoryboardSegue, sender: Any?) {


        guard segue.identifier == "MessageNow", let chatVc = segue.destination as? SendMessageViewController else {
            return
        }
chatVc.username = self.username
}}

但是当我点击任何单元格时,它总是显示我点击的第一个单元格的名称。

【问题讨论】:

    标签: ios swift firebase firebase-realtime-database


    【解决方案1】:

    这是你在准备segue方法时需要做的,你可以从选中的单元格中获取用户名并将其设置为chatVC

    override public func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        guard segue.identifier == "MessageNow", let chatVc = segue.destination as? SendMessageViewController else {
            return
        }
        let selectedIndexPath = tableView.indexPathForSelectedRow
        let cell = tableView.cellForRow(at: selectedIndexPath) as! YourCustomCell
    
        chatVc.username = cell.userNameLabelPropertyOfYourCell.text
    }
    

    【讨论】:

    • 不客气,如果您仅使用 viewcontroller "username" 变量设置为 chatVC,您可以将其删除。
    【解决方案2】:

    您必须在tableView(_:didSelectRowAt:) 方法中将值分配给self.username。这些变化应该反映在那时。

    我假设您在用户选择 tableView 行时触发 segue,并且当用户选择 tableView 行时您没有将新值分配给self.username,因此它会继续显示旧值。

    【讨论】:

    • 我在 didSelectRowAt 中做了完全相同的事情,但它仍然无法正常工作
    【解决方案3】:

    您可以使用 UITableViewDelegate 的 'didSelectRowAt' 进行归档

    试试这个:

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
         guard let cell = tableView.cellForRow(at: indexPath) as? MessageTableViewCell else {
             return
         }
         self.username = cell.username
    }
    

    【讨论】:

    • 单元格没有属性用户名
    • 我编辑了我的答案。您需要将其转换为您使用的 UITableViewCell。 (MessageTableViewCell) 这个单元格有一个显示你的用户名的属性。
    • 我的意思是在名为 username 的单元格中没有变量
    • 我认为 TableViewCell 已在某处的单元格中分配了用户名。这就是您在问题中描述的内容。
    猜你喜欢
    • 2013-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多