在TableView上添加搜索栏但没有响应

时间:2018-05-28 11:57:57

标签: ios swift

参考shrikar的工作 - ://shrikar.com/swift-ios-tutorial-uisearchbar-and-uisearchbardelegate/,我尝试在表格视图中构建一个搜索栏。没有添加搜索文本时,tableview工作正常。添加搜索文本时问题是没有响应和错误消息。

我想知道有什么不对劲。有人可以帮助我们吗?感谢。

import UIKit
import SDWebImage         
class ArticleListViewController: UITableViewController, UISearchBarDelegate{

@IBOutlet weak var searchBar: UISearchBar!

    var searchActive : Bool = false
    var filtered = [Article]()

    var articles = [Article](){
        didSet{
            DispatchQueue.main.async{
                self.tableView.reloadData()
            }
        }
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        searchBar.delegate = self
        downLoadLatestArticles()
    }
    func downLoadLatestArticles(){
        Article.downLoadItem { (articles, error) in
            if let error = error {
                print("fail \(error)")
                return
            }
            if let articles = articles {
                self.articles = articles
            }
        }
    }
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) {

        if(searchText == " "){
            searchActive = false
        }else{
                filtered = articles.filter ({ (article_f) -> Bool in
                let tmp: String = article_f.name
                let range = tmp.range(of:searchText, options: String.CompareOptions.caseInsensitive)
                return range != nil
            })

            if(filtered.count == 0){
                searchActive = false;
            } else {
                searchActive = true;
            }
        }
        self.tableView.reloadData()
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

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

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "ListTableCell", for: indexPath) as! ListTableCell

        var article : Article

        if(searchActive){
            article = filtered[indexPath.row]
        }
        else{
            article = articles[indexPath.row]
        }

        cell.nameLabel?.text = article.name
        cell.locationLabel?.text = article.location
        cell.photoView?.sd_setImage(with: article.image_URL)

        return cell
    }
}

0 个答案:

没有答案