简体   繁体   English

在Swift 3中更改UITextField中的文本时调用Web服务

[英]Calling web service when change the text in UITextField in Swift 3

I am doing something like this. 我正在做这样的事情。

  1. In my viewdidAppear() I am calling my web service and getting data. 在我的viewdidAppear()我正在调用Web服务并获取数据。

     override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(true) if(dm.shouldRefresh) { com.showProgress() self.getStaffData() { (status) in self.com.removeProgress() if(status) { if(self.arrayDirectory != nil && self.arrayDirectory.count>0) { print("-----Directory data loaded----") self.arrayPreviouslyLoadedArray=self.arrayDirectory if(self.isfirstTime) { self.setupFaces() self.isfirstTime=false } else { self.facesCarousel.reloadData() } } else { self.addEmptyLabel() } } else { self.com.showAlertMessage() self.addEmptyLabel() } } self.dm.shouldRefresh=false } } 
  2. After my table loaded, I want to perform search function on this. 加载表后,我想对此执行搜索功能。 To search I used UITextField 为了搜索,我使用了UITextField

     func searchTextFieldEditingChanged() { if(txtSearch.text != "") { self.searchText=txtSearch.text //NSObject.cancelPreviousPerformRequests(withTarget: self, selector: #selector(search), object: nil) self.search() } else { self.arrayDirectory=self.arrayPreviouslyLoadedArray facesCarousel.reloadData() } } 

This works fine so far. 到目前为止,这个工作正常。 But the problem comes when it cannot find the matching string in the current array. 但是问题出在无法在当前数组中找到匹配的字符串时。 Initially I load 20 records from the service. 最初,我从该服务加载20条记录。 And I am searching for the name "David". 我正在搜索名称“ David”。 But this name is not included within my first 20 records. 但是这个名字不包含在我的前20条记录中。 It's available in my next 20 records. 在我的下20条记录中可用。 But my service doesn't have a searching facility. 但是我的服务没有搜索工具。 But how can I perform this search function in normal way? 但是,如何以常规方式执行此搜索功能? Basically what I want is 基本上我想要的是

  1. First load 20 records 首次加载20条记录
  2. reload my table 重新加载我的桌子
  3. type search query in text field 在文本字段中输入搜索查询
  4. check whether that text available within my first 20 records 检查该文本在我的前20条记录中是否可用
  5. if available, reload my table 如果有的话,重新加载我的桌子
  6. if not available take the next 20 records 如果不可用,请记录下20条记录
  7. perform the search 执行搜索
  8. if not available take the next 20 from the service and perform the search 如果不可用,请从服务中获取下20个并执行搜索

This is what I wanna do. 这就是我想做的。 And I have done up to 4th point. 我已经完成了第四点。 That means I can search a word from my first 20 records. 这意味着我可以从前20条记录中搜索一个单词。 I tried to set a flag and run the service call till its getting true. 我试图设置一个标志并运行服务调用,直到实现为止。

func search()
{
    self.isSearching=true
    self.isfound=false
    //let exists = NSPredicate(format: "DisplayName2 CONTAINS[cd] \(searchText!)")
    let arrayTofilter:[[String:Any]]=self.arrayDirectory as! [[String : Any]]
    let aList = arrayTofilter.filter {
        ( $0 ["DisplayName2"] as! String).range(of: searchText!, options: [.diacriticInsensitive, .caseInsensitive]) != nil
    }

    if(aList.count>0)
    {
        arrayDirectory=aList
        facesCarousel.reloadData()
        isfound=true
    }
    else
    {
        repeat
        {
            self.loadAgain()
        }

        while isfound==false
    }
}

But this is not working as I expected. 但是,这不符合我的预期。 Please suggest me a better solution. 请给我建议一个更好的解决方案。 Thanks! 谢谢!

You don't need a flag. 您不需要标志。 To achieve your goal, all you need is to call search function again and again if you cannot find data. 为了实现您的目标,所有您需要的是在找不到数据时一次又一次调用搜索功能。 So here is the pseudo code 所以这是伪代码

func search() {
    array.findYourObject

    if found {
        tableView.reloadData()
    } else {
        self.getStaffData() { (status) in
            array += newlyFetched20Results
            search()// You probably want to add some condition here to stop it from searching forever
        }
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM