【问题标题】:Delete text on searchbar and return to view before the search. SWIFT删除搜索栏上的文本并返回到搜索前的视图。迅速
【发布时间】:2015-08-07 14:07:12
【问题描述】:

我已经有一些在 tableview 上显示的项目。当我使用搜索栏进行搜索时,相同的表格视图会更新为新项目。(我今天的项目)。

但是如果我删除了我在搜索栏上写的内容,我需要返回它们之前的项目。 我怎样才能做到这一点 ?

     import UIKit
     import Alamofire

 class MovieViewController: UIViewController, AsyncUpdateProtocol,UISearchBarDelegate, UITableViewDelegate, UITableViewDataSource {


@IBOutlet weak var myTable: UITableView!
@IBOutlet weak var searchBar: UISearchBar!
var myArray: Array<Movie>!
var movieData: MovieData!


override func viewDidLoad() {
    super.viewDidLoad()
    searchBar.delegate = self
    self.myTable.dataSource = self
    self.movieData = MovieData(controllerUpdate: self)
    self.myArray = self.movieData.data
}


func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return self.movieData.data.count;
}


func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell: AnyObject = self.myTable.dequeueReusableCellWithIdentifier("Cell") as! UITableViewCell
    cell.textLabel!!.text = self.movieData.data[indexPath.row].title
    cell.textLabel!!.adjustsFontSizeToFitWidth = true
    var url: NSURL
    if((self.movieData.data[indexPath.row].poster ) != nil){
        url = NSURL(string: "http://image.tmdb.org/t/p/w500\(self.movieData.data[indexPath.row].poster)") as NSURL!
    }else{
        url = NSURL(string: "http://developer-agent.com/wp-content/uploads/2015/05/images_no_image_jpg3.jpg") as NSURL!
    }
    var data = NSData(contentsOfURL: url) as NSData!
    var imagem = UIImage(data: data!)
    cell.imageView!!.image = imagem
    return cell as! UITableViewCell
}
 func searchBarSearchButtonClicked(searchBar: UISearchBar){
    pesquisa(searchBar.text)
    searchBar.resignFirstResponder()
}


 func pesquisa(nome: String){
    self.movieData.data.removeAll()
    self.movieData.request(nome)
}

【问题讨论】:

  • 这里的每个人都想帮助你,但除非你把你的代码放到你的问题中,否则很难做到。
  • 我不知道会不会有很大帮助,但是就在这里。

标签: swift tableview searchbar


【解决方案1】:

您应该对 textDidChange 使用 UISearchBar 委托方法并检查 nil,如果它是 nil,那么您可以像在视图中加载一样加载数据,这就是我的意思:

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

    if searchText.isEmpty == true {

        self.movieData = MovieData(controllerUpdate: self)

    } 

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-21
    • 1970-01-01
    • 2021-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-04
    相关资源
    最近更新 更多