How do I link the search bar to display results in

2020-08-05 10:31发布

问题:

I am currently working on using a Microsoft Exchange API for an application that allows me to look up a user based on typing in part of a name. I wanted to know how to link the search bar so that it displays related terms. Below I posted my code/ attempts

func HTTPPost(aaplication: UIApplication!, didFinishLaunchingWithOptions launchOptions: NSDictionary!) -> Bool {
    var request = NSMutableURLRequest(URL: NSURL(string: "http://mail.SAP.com/EWS/Exchange.asmx")!)
    var session = NSURLSession.sharedSession()
    request.HTTPMethod = "Post"




    return true
}
@IBOutlet weak var SearchBar: UISearchBar!

I also have this:

HTTPGet("https://mail.SAP.com/EWS/Exchange.asmx") {
(data: String, error: String?) -> Void in
if error != nil {
    println(error)
    }
else {
    println(data)
    }
}

回答1:

Here is some sample code to implement a search bar to you IOS app.

import UIKit

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate{

    @IBOutlet weak var searchBar: UISearchBar!
    @IBOutlet weak var tableView: UITableView!

    var searchActive : Bool = false
    var data = ["San Francisco","New York","San Jose","Chicago","Los Angeles","Austin","Seattle"]
    var filtered:[String] = []

    override func viewDidLoad() {
        super.viewDidLoad()

        /* Setup delegates */
        tableView.delegate = self
        tableView.dataSource = self
        searchBar.delegate = self

    }

    func searchBarTextDidBeginEditing(searchBar: UISearchBar) {
        searchActive = true;
    }

    func searchBarTextDidEndEditing(searchBar: UISearchBar) {
        searchActive = false;
    }

    func searchBarCancelButtonClicked(searchBar: UISearchBar) {
        searchActive = false;
    }

    func searchBarSearchButtonClicked(searchBar: UISearchBar) {
        searchActive = false;
    }

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

        filtered = data.filter({ (text) -> Bool in
            let tmp: NSString = text
            let range = tmp.rangeOfString(searchText, options: NSStringCompareOptions.CaseInsensitiveSearch)
            return range.location != NSNotFound
        })
        if(filtered.count == 0){
            searchActive = false;
        } else {
            searchActive = true;
        }
        self.tableView.reloadData()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if(searchActive) {
            return filtered.count
        }
        return data.count;
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {


        let cell = tableView.dequeueReusableCellWithIdentifier("Cell") as UITableViewCell;


    let label:UILabel = cell.viewWithTag(1) as UILabel



        if(searchActive){
            Label.text = filtered[indexPath.row]
        } else {
            Label.text = data[indexPath.row];
        }

        return cell;
    }
}