【问题标题】:Making searchable UITableView that when a cell is selected it takes it to a different ViewController but the cell index is unchangedMaking searchable UITableView that when a cell is selected it takes it to a different ViewController but the cell index is unchanged
【发布时间】:2019-09-21 03:56:37
【问题描述】:

我正在尝试创建一个可搜索的UITableView。但是,当新结果在搜索后加载时,我希望它们原来的 indexPaths 保持与以前相同。

如果我现在运行我的代码,我的第一个单元格的索引为 0,在我搜索某些内容并且返回的单元格数量不同后,第一个单元格的索引仍然为 0。

【问题讨论】:

  • 为什么代码被从问题中删除了?

标签: ios swift uitableview uisearchbar


【解决方案1】:

ViewController 显然不是故事板中的目标视图控制器。

覆盖prepare(for segue,将索引路径作为sender 传递并在那里设置标签,类似这样

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    performSegue(withIdentifier: "gotocompanyfav", sender: indexPath)
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "gotocompanyfav" {
        let destinationController = segue.destination as! ViewController
        let indexPath = sender as! IndexPath
        destinationController.searchStruct.buttonTag = indexPath.row
    }
}

【讨论】:

  • 我更新了答案,但我不知道ViewController 是什么
  • 这对我不起作用。 ViewController 是结构体所在的 ViewController。我从ViewController to my secondViewController`(searchTableViewController 中的所需目的地)中选择。当secondViewController 的视图加载时,我调用ViewController.searchStruct.buttonTag 作为字典中指示的特定变量的索引。我也想在searchTableViewController 中调用设置这个变量,这样我就可以对同一个secondViewController 执行不同的segue。非常感谢更新这些细节的答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-02
  • 2022-12-02
  • 1970-01-01
  • 2022-12-01
相关资源
最近更新 更多