How do I call an array for Searchbar for Swift Xco

2019-09-19 13:54发布

问题:

How do I get the correct viewcontroller after selecting a tableview cell can you please help me I think there is a problem with my index

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)
    }
}

回答1:

You can try

 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?
 }


回答2:

Issue is here:

Name.text = NamesC[IndexC]

Your array is filtered, your indexC is the one you used in the filtered array.

You can define a filtered array FNamesC, and update this array when you search in your search bar and in your viewController

Name.text = FNamesC[IndexC]

I don't know why you want a global array though.



标签: ios swift xcode