首页 > 解决方案 > 当我在搜索器上搜索内容时得到错误的索引

问题描述

这是一个解释我遇到的问题的例子。

我在一个数组中有 3 个元素。数组的索引匹配每个句子。喜欢

0 索引匹配“你好”(0 索引) 1 索引匹配“nice”(1 索引) 2 索引匹配“你好吗?” (2个索引)

我已将 UISearchBar 放在我的 tableView 中。当我在 searchBar 中键入“h”时,“hello”和“how are you”完美显示,但问题是,当我触摸 tableview 列表上的 2 索引“how are you”时,它显示“nice”在下一个 viewController 上(我有两个 viewController,名为 myIndex 的变量使用下一个 viewController 上的索引),因为该变量获得 1 个索引而不是 2 个索引。我应该如何解决这个问题?

完整代码如下:

  import UIKit

  class tableviewtest: UIViewController, UITableViewDelegate, UITableViewDataSource {
  @IBOutlet weak var searchBar: UISearchBar!

  func tableView(_ tableView: UITableView,
               moveRowAt sourceIndexPath: IndexPath,
               to destinationIndexPath: IndexPath) {

}



var searchArr = [String]()
var searching = false

var copiedArray:[String] = eng  // eng is the original array that has the full elements from another view controller. 

let cellIdentifier = "xxx"


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell:UITableViewCell = self.tableview.dequeueReusableCell(withIdentifier: cellIdentifier, for:indexPath)
    if searching {
        cell.textLabel?.text = searchArr[indexPath.row]

    }
    else {
        cell.textLabel?.text = copiedArray[indexPath.row]

    }
    return cell
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if searching {
        return searchArr.count
    }
    else {
    return copiedArray.count
}
}

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

   myIndex = indexPath.row

    performSegue(withIdentifier: "segue1", sender: self)
}


@IBOutlet weak var tableview: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()

    tableview.delegate = self
    tableview.dataSource = self
    self.tableview.register(UITableViewCell.self,forCellReuseIdentifier: cellIdentifier)

  }

  }

extension tableviewtest: UISearchBarDelegate {
func searchBar (_ searchBar:UISearchBar, textDidChange searchText:     String) {
    searchArr = copiedArray.filter({$0.prefix(searchText.count) ==      searchText})
   searching = true
      tableview.reloadData()
  }

}

标签: iosswiftxcodetableview

解决方案


在您从数组中获取字符串的那一刻,您必须考虑searching状态

let selectedItem = searching ? searchArr[myIndex] : filteredArr[myIndex]

如果搜索字符串为空,则必须重置 searching

func searchBar (_ searchBar:UISearchBar, textDidChange searchText: String) {
    if searchText.isEmpty {
        searching = false
        searchArr.removeAll()
    } else {
        searchArr = filteredArr.filter({$0.prefix(searchText.count) == searchText})
        searching = true
    }
    tableview.reloadData()
 }

你甚至可以将字符串作为sender参数传递performSegue给下一个视图控制器prepare(for segue

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

    let selectedItem = searching ? searchArr[indexPath.row] : filteredArr[indexPath.row]
    performSegue(withIdentifier: "segue1", sender: selectedItem)
}

边注:

重新考虑变量的命名:为什么数据源数组一次被调用filteredArr然后numberOfRows,为什么非过滤数组仍然被调用 filteredArr


推荐阅读