Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class MyTableViewController: UITableViewController {
- var pokemons:[Charact?] = []
- var pokemons_sorted:[Charact?] = []
- var images : [UIImage?] = []
- let MAX_POKEMONS = 50
- var imagesDownload = 0
- var connection = Cone()
- @IBOutlet weak var editButton: UIBarButtonItem!
- override func viewDidLoad() {
- super.viewDidLoad()
- // Uncomment the following line to preserve selection between presentations
- // self.clearsSelectionOnViewWillAppear = false
- // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
- // self.navigationItem.rightBarButtonItem = self.editButtonItem
- title = "Lista"
- pokemons = [Charact?](repeating: nil, count: MAX_POKEMONS)
- images = [UIImage?](repeating: nil, count: MAX_POKEMONS)
- tableView.alpha = 0
- downloadPokemonsInfo()
- }
- // MARK: - Table view data source
- override func numberOfSections(in tableView: UITableView) -> Int {
- // #warning Incomplete implementation, return the number of sections
- return 1
- }
- override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
- // #warning Incomplete implementation, return the number of rows
- return pokemons.count
- }
- override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
- let cell = tableView.dequeueReusableCell(withIdentifier: "myPokemonCell", for: indexPath) as! MyTableViewCell
- // Configure the cell...
- if let pokemon = pokemons[indexPath.row]{
- cell.pokemonLabel.font = UIFont(name: "Pokemon Solid", size: 11)
- cell.pokemonLabel.text = pokemon.name
- }
- if let image = images[indexPath.row]{
- cell.pokemonImage.image = image
- }
- return cell
- }
- // Override to support conditional editing of the table view.
- override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
- // Return false if you do not want the specified item to be editable.
- return true
- }
- // Override to support editing the table view.
- override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
- if editingStyle == .delete {
- // Delete the row from the data source
- pokemons.remove(at: indexPath.row)
- images.remove(at: indexPath.row)
- tableView.deleteRows(at: [indexPath], with: .fade)
- }
- }
- // Override to support rearranging the table view.
- override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) {
- let pokemonToMove = pokemons[fromIndexPath.row]
- let imageToMove = images[fromIndexPath.row]
- pokemons.remove(at: fromIndexPath.row)
- images.remove(at: fromIndexPath.row)
- pokemons.insert(pokemonToMove, at: to.row)
- images.insert(imageToMove, at: to.row)
- }
- // Override to support conditional rearranging of the table view.
- override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
- // Return false if you do not want the item to be re-orderable.
- return true
- }
- // MARK: - Navigation
- // In a storyboard-based application, you will often want to do a little preparation before navigation
- override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
- // Get the new view controller using segue.destination.
- // Pass the selected object to the new view controller.
- if segue.identifier == "veADetalleSegue" {
- if let detailVC = segue.destination as? DetailViewController,
- let indexPath = tableView.indexPathForSelectedRow,
- let pokemon = pokemons[indexPath.row],
- let image = images[indexPath.row]{
- detailVC.characImage = image
- detailVC.character = pokemon
- }
- }
- }
- func downloadPokemonsInfo(){
- for i in 1...MAX_POKEMONS{
- connection.getCharact(withId: i) { charact in
- if let charact = charact, let id = charact.id{
- self.pokemons[id-1] = charact
- if let imageURL = charact.sprites?.frontDefault{
- self.connection.getSprite(withURLString: imageURL) { image in
- self.imagesDownload = self.imagesDownload + 1
- if let image = image {
- self.images[id - 1] = image
- }
- if self.imagesDownload == self.MAX_POKEMONS{
- DispatchQueue.main.async {
- self.tableView.reloadData()
- self.tableView.alpha = 1
- }
- }
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement