2017-06-21 75 views
0

我在我的表格視圖中有搜索欄我想在搜索欄中自動搜索或自動粘貼文本時,我們已經複製文本如何在swift中自動粘貼文本?

我可以做到嗎?

它的源代碼

進口的UIKit

類TableViewController:的UITableViewController,UISearchBarDelegate {

var listEnWord = [EnWordModel]() 
var filteredWord = [EnWordModel]() 
var inSearchMode = false 
var dbHelper = DatabaseHelper() 

override func viewDidLoad() { 
    super.viewDidLoad() 
    searchaWord() 
    loadAllWords() 

    } 

override func viewWillAppear(_ animated: Bool) { 
    super.viewWillAppear(animated) 
    self.navigationController?.setNavigationBarHidden(false, animated: animated) 
} 

func loadAllWords(){ 
    listEnWord = dbHelper.getAllEnWord() 
    tableView.dataSource = self 
    tableView.delegate = self 
    } 


func searchaWord(){ 
    let searchBar = UISearchBar() 
    self.view.addSubview (searchBar) 
    searchBar.delegate = self 
    searchBar.returnKeyType = UIReturnKeyType.done 
    navigationItem.titleView = searchBar 
} 


func searchBarCancelButtonClicked(_ searchBar: UISearchBar) { 
    self.view.endEditing(true) 
} 

override func numberOfSections(in tableView: UITableView) -> Int { 
    return 1 
} 

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 

    if inSearchMode { 
     return filteredWord.count 
    } 

    return listEnWord.count 
} 


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

    if inSearchMode { 
     cell.enword = filteredWord[indexPath.row] 

    } else { 

     cell.enword = listEnWord[indexPath.row] 

    } 

    return cell 
} 

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

    if searchBar.text == nil || searchBar.text == "" { 
     inSearchMode = false 
     tableView.reloadData() 
     self.view.endEditing(true) 

    } else { 

     inSearchMode = true 
     let lower = searchBar.text!.lowercased() 
     filteredWord = listEnWord.filter({$0.Word?.range(of: lower, options: .anchored) != nil}) 
     tableView.reloadData() 
     tableView.setContentOffset(CGPoint.zero, animated: true) 
     self.view.endEditing(true) 
    } 
} 

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
    if inSearchMode { 
     Singleton.ShareInstance.enwordSelected = filteredWord[indexPath.row] 
    } else { 
     Singleton.ShareInstance.enwordSelected = listEnWord[indexPath.row] 
    } 

    let des = storyboard?.instantiateViewController(withIdentifier: "DetailController") 
    navigationController?.pushViewController(des!, animated: true) 
} 

}

感謝幫助我

回答

2

當然,好像你正在尋找一種自動搜索的方式文本在剪貼板中。所以首先以這種方式註冊剪貼板文本更改監聽器。請注意,它只是你的應用程序中的工作原理:

NotificationCenter.default.addObserver(self, selector: #selector(clipboardChanged), 
             name: NSNotification.Name.UIPasteboardChanged , object: nil) 

然後通過此功能處理它

func clipboardChanged(){ 
    let pasteboardString: String? = UIPasteboard.general.string 
    if let theString = pasteboardString { 
     print("String is \(theString)") 
     // Put the string into your search bar and do the search 
    } 
}