在单击不同的 TableView 单元之前,TableView 单元不加载视图控制器

TableView Cell not loading View Controller until Different TableView Cell is clicked

我四处搜索,并没有真正找到发生这种情况的原因。基本上,我按照 Jared Davison 的这篇教程 https://www.youtube.com/watch?v=yupIw9FXUso 创建了 Table View Cells to Multiple View controller。在他的示例中,一切正常,但由于某种原因,当您单击我代码中的 table 视图单元格时,该单元格以灰色突出显示。然后,当用户点击一个单独的 table 视图单元时,应该加载第一个 table 视图单元的视图控制器被加载。如果用户随后单击返回原始 table 视图单元格,则加载本应由第二个 table 视图单元格加载的页面。总之,所有的视图控制器都在 "click" 之后加载。

这是 table 视图的代码:

//Feed Navigation Functions
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return elements.count
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 75
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "feedCell") as! FeedTableViewCell
    cell.txtTitle.text = "The Fight Against \(elements[indexPath.row])"
    cell.issueIcon.image = UIImage(named: "Issue Icons_\(elements[indexPath.row])")
    return cell
}
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
    let vcName = identities[indexPath.row]
    let viewController = storyboard?.instantiateViewController(withIdentifier: vcName)
    self.navigationController?.pushViewController(viewController!, animated: true)
}

更新:数组是一个简单的字符串数组,例如[One, Two, Three]数组中有 6 个字符串。

问题:click on a table view cell in my code the cell is highlighted in grey 这是由于 selectionStyle,您可以阅读 here。如果不想单元格高亮,可以设置cell.selectionStyle = .none.

编辑:如其他正确回复中所示 - 问题出在方法中的 incorrect/typo - 我们应该使用 didSelectRowAt 而不是 didDeselectRowAt.

您想使用 tableView 的委托方法 didSelectRow 而不是 didDeselectRow 我认为...

当你 select 一个单元格然后 select 另一个单元格时,方法 didDeselectRow 被调用所以 Vc 被推送你实际上想要实现 didSelectRowAt

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    let vcName = identities[indexPath.row]
    let viewController = storyboard?.instantiateViewController(withIdentifier: vcName)
    self.navigationController?.pushViewController(viewController!, animated: true)

    // this to deSelect it after push
    tableView.deselectRow(at: indexPath, animated: false)

}

此方法在 selected

单元格时触发

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath

取消选择单元格时会触发此方法

func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath