Advertisement
Guest User

Untitled

a guest
May 26th, 2022
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.39 KB | None | 0 0
  1. class MyTableViewController: UITableViewController {
  2.  
  3. var pokemons:[Charact?] = []
  4. var pokemons_sorted:[Charact?] = []
  5.  
  6. var images : [UIImage?] = []
  7. let MAX_POKEMONS = 50
  8. var imagesDownload = 0
  9. var connection = Cone()
  10.  
  11. @IBOutlet weak var editButton: UIBarButtonItem!
  12.  
  13.  
  14.  
  15. override func viewDidLoad() {
  16. super.viewDidLoad()
  17.  
  18. // Uncomment the following line to preserve selection between presentations
  19. // self.clearsSelectionOnViewWillAppear = false
  20.  
  21. // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
  22. // self.navigationItem.rightBarButtonItem = self.editButtonItem
  23.  
  24. title = "Lista"
  25.  
  26.  
  27.  
  28. pokemons = [Charact?](repeating: nil, count: MAX_POKEMONS)
  29.  
  30.  
  31.  
  32. images = [UIImage?](repeating: nil, count: MAX_POKEMONS)
  33.  
  34. tableView.alpha = 0
  35. downloadPokemonsInfo()
  36. }
  37.  
  38. // MARK: - Table view data source
  39.  
  40. override func numberOfSections(in tableView: UITableView) -> Int {
  41. // #warning Incomplete implementation, return the number of sections
  42. return 1
  43. }
  44.  
  45. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  46. // #warning Incomplete implementation, return the number of rows
  47. return pokemons.count
  48. }
  49.  
  50.  
  51. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  52. let cell = tableView.dequeueReusableCell(withIdentifier: "myPokemonCell", for: indexPath) as! MyTableViewCell
  53.  
  54. // Configure the cell...
  55. if let pokemon = pokemons[indexPath.row]{
  56. cell.pokemonLabel.font = UIFont(name: "Pokemon Solid", size: 11)
  57. cell.pokemonLabel.text = pokemon.name
  58.  
  59. }
  60. if let image = images[indexPath.row]{
  61. cell.pokemonImage.image = image
  62. }
  63.  
  64.  
  65. return cell
  66. }
  67.  
  68.  
  69.  
  70. // Override to support conditional editing of the table view.
  71. override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
  72. // Return false if you do not want the specified item to be editable.
  73. return true
  74. }
  75.  
  76.  
  77.  
  78. // Override to support editing the table view.
  79. override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
  80. if editingStyle == .delete {
  81. // Delete the row from the data source
  82. pokemons.remove(at: indexPath.row)
  83. images.remove(at: indexPath.row)
  84. tableView.deleteRows(at: [indexPath], with: .fade)
  85. }
  86. }
  87.  
  88.  
  89.  
  90. // Override to support rearranging the table view.
  91. override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) {
  92.  
  93. let pokemonToMove = pokemons[fromIndexPath.row]
  94. let imageToMove = images[fromIndexPath.row]
  95.  
  96. pokemons.remove(at: fromIndexPath.row)
  97. images.remove(at: fromIndexPath.row)
  98.  
  99. pokemons.insert(pokemonToMove, at: to.row)
  100. images.insert(imageToMove, at: to.row)
  101.  
  102. }
  103.  
  104.  
  105.  
  106. // Override to support conditional rearranging of the table view.
  107. override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
  108. // Return false if you do not want the item to be re-orderable.
  109. return true
  110. }
  111.  
  112.  
  113. // MARK: - Navigation
  114.  
  115. // In a storyboard-based application, you will often want to do a little preparation before navigation
  116. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  117. // Get the new view controller using segue.destination.
  118. // Pass the selected object to the new view controller.
  119. if segue.identifier == "veADetalleSegue" {
  120. if let detailVC = segue.destination as? DetailViewController,
  121. let indexPath = tableView.indexPathForSelectedRow,
  122. let pokemon = pokemons[indexPath.row],
  123. let image = images[indexPath.row]{
  124. detailVC.characImage = image
  125. detailVC.character = pokemon
  126. }
  127.  
  128.  
  129. }
  130. }
  131.  
  132.  
  133.  
  134. func downloadPokemonsInfo(){
  135. for i in 1...MAX_POKEMONS{
  136. connection.getCharact(withId: i) { charact in
  137. if let charact = charact, let id = charact.id{
  138. self.pokemons[id-1] = charact
  139. if let imageURL = charact.sprites?.frontDefault{
  140. self.connection.getSprite(withURLString: imageURL) { image in
  141. self.imagesDownload = self.imagesDownload + 1
  142. if let image = image {
  143. self.images[id - 1] = image
  144. }
  145. if self.imagesDownload == self.MAX_POKEMONS{
  146. DispatchQueue.main.async {
  147. self.tableView.reloadData()
  148. self.tableView.alpha = 1
  149. }
  150. }
  151. }
  152. }
  153. }
  154. }
  155. }
  156.  
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement