mardi 12 septembre 2017

To get random image from Parse

I tried many code asked in stack overflow but almost all of them is for core data not from parse.

I try to get random image from Parse at kombin.swift. Then with a button to change the image randomly. Which code should I use to have image randomly at first and then change the image(with a button) randomly?

This is my ViewController:

 import UIKit
import Parse

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {


    @IBOutlet weak var tableView: UITableView!

    var placeNameArray = [String]()
    var chosenPlace = ""


    override func viewDidLoad() {


        super.viewDidLoad()

        tableView.delegate = self
        tableView.dataSource = self


        getData()
    }







    func getData() {

        let query = PFQuery(className: "Places")
        query.findObjectsInBackground  { (objects, error) in
            if error != nil {

                let alert = UIAlertController(title: "Error", message: error?.localizedDescription, preferredStyle: UIAlertControllerStyle.alert)
                let okButton = UIAlertAction(title: "OK", style: UIAlertActionStyle.cancel, handler: nil)
                alert.addAction(okButton)
                self.present(alert, animated: true, completion:nil)


            } else {

                self.placeNameArray.removeAll(keepingCapacity: false)
                for object in objects! {

                    self.placeNameArray.append(object.object(forKey: "name") as! String)

                }
                self.tableView.reloadData()

            }
        }


    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

        if segue.identifier == "1to4"

        {
            let destinationVC = segue.destination as! ViewController4
            destinationVC.selectedPlace = self.chosenPlace
        }

    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){


        self.chosenPlace = placeNameArray[indexPath.row]
        self.performSegue(withIdentifier: "1to4", sender: nil)
    }



    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell()
        cell.textLabel?.text = placeNameArray[indexPath.row]
        return cell


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

        return placeNameArray.count

    }



    @IBAction func ekle(_ sender: Any) {
        performSegue(withIdentifier: "1to2", sender: nil)
    }

}

my kombin.swift:

import UIKit
import Parse

class kombin: UIViewController {



    @IBOutlet weak var imageView: UIImageView!


    var imageArray = [PFFile]()
    var placeNameArray = [String]()


    override func viewDidLoad() {
        super.viewDidLoad()


        func randomImage() -> UIImage {
            let unsignedArrayCount = UInt32(imageArray.count)
            let unsignedRandomNumber = arc4random_uniform(unsignedArrayCount)
            let randomNumber = Int(unsignedRandomNumber)
            return UIImage(named: placeNameArray[randomNumber])!
        }




    }




Aucun commentaire:

Enregistrer un commentaire