Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 删除搜索栏上的文本并在搜索前返回查看。敏捷的_Swift_Tableview_Searchbar - Fatal编程技术网

Swift 删除搜索栏上的文本并在搜索前返回查看。敏捷的

Swift 删除搜索栏上的文本并在搜索前返回查看。敏捷的,swift,tableview,searchbar,Swift,Tableview,Searchbar,我已经在tableview上显示了一些项目。当我使用searchbar进行搜索时,相同的tableview会更新为新项目(我今天的项目) 但是如果我删除了我在搜索栏上写的内容,我需要返回以前的项目。 我该怎么做 import UIKit import Alamofire class MovieViewController: UIViewController, AsyncUpdateProtocol,UISearchBarDelegate, UITableViewDeleg

我已经在tableview上显示了一些项目。当我使用searchbar进行搜索时,相同的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)
}
导入UIKit
进口阿拉莫菲尔
类MovieViewController:UIViewController、AsyncUpdateProtocol、UISearchBarDelegate、UITableViewDelegate、UITableViewDataSource{
@ibvar myTable:UITableView!
@ibvar搜索栏:UISearchBar!
var myArray:Array!
var movieData:movieData!
重写func viewDidLoad(){
super.viewDidLoad()
searchBar.delegate=self
self.myTable.dataSource=self
self.movieData=movieData(控制器更新:self)
self.myArray=self.movieData.data
}
func tableView(tableView:UITableView,numberofrowsinssection:Int)->Int{
返回self.movieData.data.count;
}
func tableView(tableView:UITableView,cellForRowAtIndexPath:nsindepath)->UITableView单元格{
让cell:AnyObject=self.myTable.dequeueReusableCellWithIdentifier(“cell”)作为!UITableViewCell
cell.textlab!!.text=self.movieData.data[indexath.row].title
cell.textLabel!!.adjustsFontSizeToFitWidth=true
var url:NSURL
if((self.movieData.data[indexath.row].poster)!=nil){
url=NSURL(字符串:”http://image.tmdb.org/t/p/w500\(self.movieData.data[indexPath.row.poster]”)作为NSURL!
}否则{
url=NSURL(字符串:”http://developer-agent.com/wp-content/uploads/2015/05/images_no_image_jpg3.jpg)作为NSURL!
}
var data=NSData(contentsofull:url)作为NSData!
var imagem=UIImage(数据:data!)
cell.imageView!!.image=imagem
返回单元格为!UITableViewCell
}
func searchBarSearchButtonClicked(搜索栏:UISearchBar){
pesquisa(searchBar.text)
searchBar.resignFirstResponder()辞职
}
func pesquisa(名称:字符串){
self.movieData.data.removeAll()
self.movieData.request(nome)
}

您应该使用UISearchBar委托方法进行textDidChange并检查是否为nil,如果为nil,则可以像在视图中加载一样加载数据,我的意思是:

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

    if searchText.isEmpty == true {

        self.movieData = MovieData(controllerUpdate: self)

    } 

}

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