Ios 如何调用Swift Xcode搜索栏的数组

Ios 如何调用Swift Xcode搜索栏的数组,ios,swift,xcode,Ios,Swift,Xcode,在选择tableview单元格后,如何获得正确的viewcontroller?请帮助我,我认为我的索引有问题 import UIKit var NamesC = [ "one", "two"] var IndexC = 0 class ComputerVC: UIViewController ,UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate{ @IBOutlet weak var searchBar: U

在选择tableview单元格后,如何获得正确的viewcontroller?请帮助我,我认为我的索引有问题

import UIKit
var NamesC =  [ "one", "two"]

var IndexC = 0
class ComputerVC: UIViewController ,UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate{
    @IBOutlet weak var searchBar: UISearchBar!
    @IBOutlet weak var TableView: UITableView!

    var data = [ "one", "two"]

    var filteredData = [String]()

    var inSearchMode = false

    override func viewDidLoad() {
        super.viewDidLoad()
        TableView.delegate = self
        TableView.dataSource = self
        searchBar.delegate = self
        searchBar.returnKeyType = UIReturnKeyType.done
        // Do any additional setup after loading the view.
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if inSearchMode {

            return filteredData.count
        }

        return data.count

    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as? DataCellC {

            let text: String!

            if inSearchMode {

                text = filteredData[indexPath.row]

            } else {

                text = data[indexPath.row]
            }

            cell.congigureCell(text: text)

            return cell

        } else {

            return UITableViewCell()
        }

    }
    func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {

        if searchBar.text == nil || searchBar.text == "" {

            inSearchMode = false

            view.endEditing(true)

            TableView.reloadData()

        } else {

            inSearchMode = true

            filteredData = data.filter({$0.contains(searchBar.text!)})

            TableView.reloadData()
        }
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
    {
        IndexC = indexPath.row
        performSegue(withIdentifier: "segue", sender: self)
    }
}
你可以试试

 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
 {
     var text = ""

    if inSearchMode {

        text = filteredData[indexPath.row]

        let correctIndex = data.index(of:filteredData[indexPath.row])

        print("selected value is : \(correctIndex)")


    } else {

        text = data[indexPath.row]
    }


    performSegue(withIdentifier: "segue", sender: text)
 }
 override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
     if let secondController = segue.destination as? SecondController {
         secondController.sendedValue =  sender as! String
     }
 }

 class secondController:UIViewController
 {
   var sendedValue:String?
 }
问题是:

Name.text=NamesC[IndexC]

您的数组已过滤,您的indexC就是您在过滤数组中使用的indexC

您可以定义一个过滤数组
FNamesC
,并在搜索栏和viewController中搜索时更新此数组

Name.text=FNamesC[IndexC]


我不知道您为什么想要一个全局数组。

您还没有指定您面临的实际问题。我假设在显示过滤后的数据时没有得到正确的值。如果是这种情况,请记住您必须在
didSelectRow
prepareForSegue
中适当地使用
inSearchMode
(如果有)。正如您在代码的其余部分中所做的一样,我面临的问题是,在搜索某些内容后,我选择了这样的内容,但当我选择单元格时,我没有获得正确的值,即单元格的视图控制器。我如何更正此问题请帮助我如何在DidSelectRow中使用inSearchMode您的要求是什么?你所说的“要求”是什么意思?我想在搜索和选择单元格后获得正确的视图控制器。你想发送什么?我有一个全局数组,在类中包含名称和描述。我有一个名称数组,然后我有搜索代码。我可以搜索很好。我在表视图控制器中得到正确的结果,但是当我选择单元格并转到其他三个控制器时,我得到了错误的索引中第一个单元格的内容请帮助请帮助我这是我的毕业设计:(你想得到filterData中显示的数据数组的索引吗???它不起作用:(我添加了更多信息,也许您知道问题出在哪里请帮助我正在使用全局数组传递数据请帮助我解决此问题