【问题标题】:TableView Cell not loading View Controller until Different TableView Cell is clicked在单击不同的 TableView 单元之前,TableView 单元不会加载视图控制器
【发布时间】:2018-06-09 16:37:27
【问题描述】:

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

这是表格视图的代码:

//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 个字符串。

【问题讨论】:

    标签: ios swift uitableview segue pushviewcontroller


    【解决方案1】:

    当您选择一个单元格然后选择另一个单元格时,方法 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)
    
    }
    

    选择单元格时触发此方法

    func tableView(_tableView: UITableView, didSelectRowAt indexPath: IndexPath

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

    func tableView(_tableView: UITableView, didDeselectRowAt indexPath: IndexPath

    【讨论】:

    • 你的代码和我的一模一样。我不明白你认为我应该改变什么?
    • 把这个 didDeselectRowAt 改成这个 didSelectRowAt ,,, 仔细看方法名
    • 我才意识到这一点。当您完全错过最重要的部分时,感觉就像是一个完全的面部时刻。快速补充一下这个问题:现在当我使用后退按钮从视图控制器返回时,单元格仍然突出显示有没有办法改变它?
    • 这是正确的回答。应该使用didSelectRowAt 而不是您当前使用的didDeselectRowAt
    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

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

      编辑:如其他正确响应所示 - 问题是方法中的不正确/错字 - 我们应该使用 didSelectRowAt 而不是 didDeselectRowAt

      【讨论】:

      • 我将其更改为仅使用一个数组,但问题仍然存在。
      • 您能否更新您的问题以包括您如何填充数据数组?
      • 我确实更新了这个问题。希望这是你想要的。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-08-22
      • 1970-01-01
      • 2016-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-25
      相关资源
      最近更新 更多