Search as you type Swift

后端 未结 3 702
眼角桃花
眼角桃花 2021-02-09 17:18

I try to implement Search as you type written in Swift. It works already but I need some tuning. I send with each letter typed in

func searchBar(searchBar: UISe         


        
3条回答
  •  时光说笑
    2021-02-09 18:15

    You could use an NSTimer. Start or invalidate and restart the timer for 2 seconds in your textDidChange. Have the timer fire the findUser method when it actually pops.

        var debounceTimer: NSTimer?
    
    @IBAction func test() {
        if let timer = debounceTimer {
            timer.invalidate()
        }
        debounceTimer = NSTimer(timeInterval: 2.0, target: self, selector: Selector("getUser"), userInfo: nil, repeats: false)
        NSRunLoop.currentRunLoop().addTimer(debounceTimer!, forMode: "NSDefaultRunLoopMode")
    }
    
    func getUser() {
        println("yeah")
    }
    

提交回复
热议问题