Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.52 KB | None | 0 0
  1. //
  2. // TableViewControllerSearch.swift
  3. // A43App
  4. //
  5. // Created by Yuri Boschetto on 20/11/2019.
  6. // Copyright © 2019 Bosyu. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10. import Alamofire
  11. import SwiftyJSON
  12.  
  13. class TableViewControllerSearch: UITableViewController,UISearchBarDelegate {
  14.  
  15.  
  16. var indicator : UIActivityIndicatorView!
  17. var arrayImmagini = [[String:AnyObject]]()
  18. var swiftyJSON: JSON!
  19.  
  20. @IBOutlet weak var searchBar1: UISearchBar!
  21.  
  22. override func viewDidLoad() {
  23. super.viewDidLoad()
  24.  
  25. // Uncomment the following line to preserve selection between presentations
  26. // self.clearsSelectionOnViewWillAppear = false
  27.  
  28. // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
  29. // self.navigationItem.rightBarButtonItem = self.editButtonItem
  30. self.searchBar1.delegate = self
  31. self.tableView.delegate = self
  32. self.tableView.dataSource = self
  33.  
  34. self.indicator = UIActivityIndicatorView(style: .gray)
  35. view.addSubview(indicator)
  36. self.indicator.center = CGPoint(x: view.frame.size.width*0.5, y: view.frame.height*0.5)
  37.  
  38. }
  39.  
  40. // MARK: - Table view data source
  41.  
  42. override func numberOfSections(in tableView: UITableView) -> Int {
  43. // #warning Incomplete implementation, return the number of sections
  44. return 1
  45. }
  46.  
  47. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  48. // #warning Incomplete implementation, return the number of rows
  49.  
  50.  
  51. return arrayImmagini.count
  52. }
  53.  
  54. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)-> UITableViewCell{
  55. let cell = self.tableView.dequeueReusableCell(withIdentifier: "cellaSearch") as! TableViewCellSearch
  56.  
  57. let imageID = arrayImmagini[indexPath.row]["ImmagineID"] as! String
  58. let email = arrayImmagini[indexPath.row]["Email"] as! String
  59. let testo = arrayImmagini[indexPath.row]["Testo"] as! String
  60.  
  61. let image1 = arrayImmagini[indexPath.row]["FileName"] as! String
  62.  
  63. // let imageFileExtension = ".jpg"
  64. let url2 = URL(string: "http://www.bosyu.altervista.org/Uploads/"+image1)
  65.  
  66. let data = try? Data(contentsOf: url2!)
  67.  
  68. cell.labelTop.text = email
  69. cell.labelBottom.text = testo
  70. cell.imageView1.image = UIImage(data: data!)
  71.  
  72.  
  73.  
  74. let imageRatio = cell.scaleImage(image: cell.imageView1.image!)
  75. tableView.rowHeight = tableView.frame.width/imageRatio+150
  76.  
  77. return cell
  78. }
  79.  
  80. func searchBarSearchButtonClicked(_ searchBar: UISearchBar){
  81. var textS = self.searchBar1.text!
  82. textS = textS.replacingOccurrences(of: " ",with:"%20",options: .literal, range: nil)
  83. let url = "http://bosyu.altervista.org/Ricerca.php?Testo="+textS
  84. self.indicator.startAnimating()
  85. Alamofire.request(url).responseJSON{
  86. response in
  87. switch response.result {
  88. case .success:
  89. self.swiftyJSON=JSON(response.result.value!)
  90. self.arrayImmagini=self.swiftyJSON["immagini"].arrayObject as! [[String:AnyObject]]
  91. self.tableView.reloadData()
  92. self.indicator.stopAnimating()
  93.  
  94.  
  95. case .failure:
  96. print(response)
  97. self.indicator.stopAnimating()
  98. }
  99. }
  100. }
  101.  
  102. /*
  103. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  104. let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath)
  105.  
  106. // Configure the cell...
  107.  
  108. return cell
  109. }
  110. */
  111.  
  112. /*
  113. // Override to support conditional editing of the table view.
  114. override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
  115. // Return false if you do not want the specified item to be editable.
  116. return true
  117. }
  118. */
  119.  
  120. /*
  121. // Override to support editing the table view.
  122. override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
  123. if editingStyle == .delete {
  124. // Delete the row from the data source
  125. tableView.deleteRows(at: [indexPath], with: .fade)
  126. } else if editingStyle == .insert {
  127. // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
  128. }
  129. }
  130. */
  131.  
  132. /*
  133. // Override to support rearranging the table view.
  134. override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) {
  135.  
  136. }
  137. */
  138.  
  139. /*
  140. // Override to support conditional rearranging of the table view.
  141. override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
  142. // Return false if you do not want the item to be re-orderable.
  143. return true
  144. }
  145. */
  146.  
  147. /*
  148. // MARK: - Navigation
  149.  
  150. // In a storyboard-based application, you will often want to do a little preparation before navigation
  151. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  152. // Get the new view controller using segue.destination.
  153. // Pass the selected object to the new view controller.
  154. }
  155. */
  156.  
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement