Swift 到单独视图控制器的数据不工作

Swift 到单独视图控制器的数据不工作,swift,tableview,cell,Swift,Tableview,Cell,因此,我试图将数据从一个单元格传递到另一个视图控制器,当我试图将其传输到“user2”时,我确实得到了“user3”中的值,但没有得到任何值。我试图打印user2,但没有返回任何内容,也没有任何错误。我怀疑这和“func prepare”有关,但我不知道是什么 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return posts.count } func

因此,我试图将数据从一个单元格传递到另一个视图控制器,当我试图将其传输到“user2”时,我确实得到了“user3”中的值,但没有得到任何值。我试图打印user2,但没有返回任何内容,也没有任何错误。我怀疑这和“func prepare”有关,但我不知道是什么

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

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
    let cell = tableView.dequeueReusableCell(withIdentifier:"searchCell", for: indexPath)
    as! CustomTableViewCell
    cell.titleField?.text = posts[indexPath.row].caption
    cell.userUID?.text = posts[indexPath.row].uid
    cell.descriptionField?.text = posts[indexPath.row].description
    cell.tagsField?.text = posts[indexPath.row].tags
    let photoUrl = posts[indexPath.row].photoUrl
    let url = URL(string: photoUrl)
    cell.SearchImage.sd_setImage(with: url, placeholderImage: nil)
    return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let indexPath = tableView.indexPathForSelectedRow
    let currentCell = tableView.cellForRow(at: indexPath!)! as! CustomTableViewCell
    let currentItem = currentCell.userUID!.text
    var user3: String = ""
    user3.append(currentItem!)



func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if let destinationViewController = segue.destination as? userViewController {
            destinationViewController.user2 = user3
        }
    }

}

我建议忘记
didSelectRowAt
,将segue连接到Interface Builder中的原型单元,而不是视图控制器

然后调用
prepare(for segue
)传递发送方参数中的表视图单元格

func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if let destinationViewController = segue.destination as? userViewController,
            let cell = sender as? CustomTableViewCell,
            let indexPath =  tableView.indexPath(for: cell) {
               let currentUID = posts[indexPath.row].uid
               destinationViewController.user2 = currentUID
        }
    }
}

始终从模型(数据源数组)中获取数据,而不是从视图(单元格)

中获取相关数据,但不要执行
让currentItem=currentCell.userUID!.text
,不要依赖于用户界面,而是依赖于您的模型。相反
让currentItem=posts[indexPath.row].uid
。对于您的问题,请断开从
UITableViewCell
创建的Segue到
userViewController
的连接。而不是从
ViewController1
userViewController
的连接,并在
中选择Rowat
使用
self.performsgue(带标识符:“segue的identifierOfTheSegue”sender:nil)
(或类似的内容)。如果您正确地检查了日志/断点,您应该注意到,在当前情况下,
prepare(for segue)
didSelectRowAt
之前调用了
prepare(for:sender)…,并确保
prepare(for:sender)
是一种顶级方法,未在另一种方法中定义。如何使didSelectRow在segue之前运行?didSelectRow在“prepare(for segue)”之前运行?这似乎可行,但我无法再单击任何单元格。除了复制代码之外,我还需要执行其他操作吗?XDNevermind我错过了您答案中的segue connect。
func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if let destinationViewController = segue.destination as? userViewController,
            let cell = sender as? CustomTableViewCell,
            let indexPath =  tableView.indexPath(for: cell) {
               let currentUID = posts[indexPath.row].uid
               destinationViewController.user2 = currentUID
        }
    }
}