如何链接搜索栏以在 swift/xcode 中显示结果 [英] How do I link the search bar to display results in swift/xcode

查看:19
本文介绍了如何链接搜索栏以在 swift/xcode 中显示结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在为一个应用程序使用 Microsoft Exchange API,该应用程序允许我根据输入姓名的一部分来查找用户.我想知道如何链接搜索栏以显示相关术语.下面我发布了我的代码/尝试

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!

我也有这个:

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

推荐答案

以下是一些示例代码,用于为您的 IOS 应用程序实现搜索栏.

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

这篇关于如何链接搜索栏以在 swift/xcode 中显示结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆