Guest User

Untitled

a guest
Feb 21st, 2018
330
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.98 KB | None | 0 0
  1. class MyViewController: UIViewController,UITableViewDataSource,UITableViewDelegate,UISearchBarDelegate {
  2.  
  3. @IBOutlet weak var tableView: UITableView!
  4.  
  5. @IBOutlet weak var searchBar: UISearchBar!
  6. var filteredArray = [String]()
  7. var shouldShowSearchResults = false
  8. var nameArray = [String]()
  9. var emailArray = [String]()
  10.  
  11. var tableData = [String]()
  12. override func viewDidLoad() {
  13. super.viewDidLoad()
  14. downloadData()
  15. createSearchBar()
  16. // Do any additional setup after loading the view.
  17. }
  18. func createSearchBar(){
  19. searchBar.showsCancelButton = false
  20. searchBar.placeholder = "Enter your search"
  21. searchBar.delegate = self
  22. self.navigationItem.titleView = searchBar
  23. }
  24.  
  25. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  26. tableData = nameArray
  27. if shouldShowSearchResults
  28. {
  29. return filteredArray.count
  30. }
  31. else
  32. {
  33. return tableData.count
  34. }
  35. }
  36. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  37. let cell:mycell! = tableView.dequeueReusableCell(withIdentifier: "cell") as! mycell
  38. if shouldShowSearchResults
  39. {
  40. cell.name.text = filteredArray[indexPath.row]
  41. cell.email.text = emailArray[indexPath.row]
  42. return cell
  43. }
  44. else
  45. {
  46. cell.name.text = tableData[indexPath.row]
  47. cell.email.text = emailArray[indexPath.row]
  48.  
  49. return cell
  50.  
  51. }
  52. }
  53. func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
  54. searchBar.endEditing(true)
  55. }
  56. func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
  57. shouldShowSearchResults = true
  58. searchBar.endEditing(true)
  59. self.tableView.reloadData()
  60. }
  61. func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
  62. filteredArray = tableData.filter({
  63. (names:String) -> Bool in
  64. return names.lowercased().range(of: searchText.lowercased()) != nil
  65. })
  66. if searchText != ""
  67. {
  68. shouldShowSearchResults = true
  69. self.tableView.reloadData()
  70. }
  71. else
  72. {
  73. shouldShowSearchResults = false
  74. self.tableView.reloadData()
  75. }
  76. }
  77. func downloadData()
  78. {
  79. let url = URL(string: "http://www.json-generator.com/api/json/get/crdvbKvLoy?indent=2")!
  80.  
  81. var request = URLRequest(url: url, cachePolicy: .reloadIgnoringCacheData, timeoutInterval: 10000)
  82.  
  83.  
  84. URLSession.shared.dataTask(with: request) { (data, response, error) in
  85. if error != nil {
  86. print(error!)
  87. return
  88. }
  89.  
  90. do {
  91. if let jsonData = try JSONSerialization.jsonObject(with:data!, options: []) as? [[String:AnyObject]] {
  92. print(jsonData)
  93. // Utility.SharedInstance.dict_UserDetails3 = jsonData as AnyObject
  94.  
  95. for item in jsonData {
  96.  
  97. if let name = item["Name"] as? AnyObject {
  98. self.nameArray.append(name as! String)
  99.  
  100. }
  101.  
  102. if let email = item["Email"] as? AnyObject{
  103. self.emailArray.append(email as! String)
  104. }
  105.  
  106. DispatchQueue.main.async {
  107. self.tableView.reloadData()
  108. }
  109. }
  110. }
  111. } catch let error as NSError {
  112. print(error)
  113. }
  114. }.resume()
  115. }
  116.  
  117. }
  118.  
  119.  
  120. output:
  121. search___________
  122. Name Email
  123. ----------------
  124. Wasim wasim@gmail.com
  125. Dravid dravid@gmail.com
  126. Kohli virat@gmail.com
  127. Kallis Jaques@gmail.com
  128.  
  129.  
  130. I entered in search text as K
  131. -----------------------------
  132. search_____K______
  133. Name Email
  134. ----------------
  135. Kohli wasim@gmail.com
  136. Kallis dravid@gmail.com
Add Comment
Please, Sign In to add comment