Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
179 views
in Technique[技术] by (71.8m points)

ios - How do I call an array for Searchbar for Swift Xcode

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)
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...