解析

时间:2015-07-24 13:17:36

标签: swift parse-platform uicollectionview

我在loadinbackground上遇到了问题。我收到一个错误,说不能调用没有参数的loadinBackground。

以下是我的代码,我已经在互联网上查看,并且不知道为什么这不起作用。

 import UIKit
import Parse
import ParseUI

class DetailViewController: UIViewController, UINavigationControllerDelegate {

    // Container to store the view table selected object
    var currentObject : PFObject?

    // Some text fields
    @IBOutlet weak var topsLabel: UITextField!
    @IBOutlet weak var topsImageView: PFImageView!

    var updateObject : PFObject?

    // The save button
    @IBAction func saveButton(sender: AnyObject) {

        // Use the sent country object or create a new country PFObject
        if let updateObjectTest = currentObject as PFObject? {
            updateObject = currentObject! as PFObject
        } else {
            updateObject = PFObject(className:"Tops")
        }

        // Update the object
        if let updateObject = updateObject {

            updateObject["topsLabel"] = topsLabel.text

            // Create a string of text that is used by search capabilites
            var searchText = (topsLabel.text).lowercaseString
            updateObject["searchText"] = searchText

            // Update the record ACL such that the new record is only visible to the current user
            updateObject.ACL = PFACL(user: PFUser.currentUser()!)

            // Save the data back to the server in a background task
            updateObject.save()
        }

        // Return to table view
        self.navigationController?.popViewControllerAnimated(true)
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        // Unwrap the current object object
        if let object = currentObject {
            if let value = object["topsLabel"] as? String {
                topsLabel.text = value


            // Display standard question image
            var initialThumbnail = UIImage(named: "question")
            topsImageView.image = initialThumbnail

            // Replace question image if an image exists on the parse platform
            if let thumbnail = object["topsImageView"] as? PFFile {
                topsImageView.file = thumbnail
                topsImageView.loadInBackground()
            }
        }
    }
}


    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}

Tops View

import UIKit
import Parse

var tops = [PFObject]()

class TopsViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UISearchBarDelegate {

    // Connection to the search bar
    @IBOutlet weak var searchBar: UISearchBar!

    // Connection to the collection view
    @IBOutlet weak var collectionView: UICollectionView!

    override func viewDidLoad() {
        super.viewDidLoad()

        // Wire up search bar delegate so that we can react to button selections
        searchBar.delegate = self

        // Resize size of collection view items in grid so that we achieve 3 boxes across
        let cellWidth = ((UIScreen.mainScreen().bounds.width) - 32 - 30 ) / 3
        let cellLayout = collectionView.collectionViewLayout as! UICollectionViewFlowLayout
        cellLayout.itemSize = CGSize(width: cellWidth, height: cellWidth)
    }

    /*
    ==========================================================================================
    Ensure data within the collection view is updated when ever it is displayed
    ==========================================================================================
    */

    // Load data into the collectionView when the view appears
    override func viewDidAppear(animated: Bool) {
        loadCollectionViewData()
    }

    /*
    ==========================================================================================
    Fetch data from the Parse platform
    ==========================================================================================
    */

    func loadCollectionViewData() {

        // Build a parse query object
        var query = PFQuery(className:"Tops")

        // Check to see if there is a search term
        if searchBar.text != "" {
            query.whereKey("uploader", containsString: searchBar.text.lowercaseString)
        }

        // Fetch data from the parse platform
        query.findObjectsInBackgroundWithBlock {
            (objects: [AnyObject]?, error: NSError?) -> Void in

            // The find succeeded now rocess the found objects into the countries array
            if error == nil {

                // Clear existing country data
                tops.removeAll(keepCapacity: true)

                // Add country objects to our array
                if let objects = objects as? [PFObject] {
                    tops = Array(objects.generate())
                }

                // reload our data into the collection view
                self.collectionView.reloadData()

            } else {
                // Log details of the failure
                println("Error: \(error!) \(error!.userInfo!)")
            }
        }
    }

    /*
    ==========================================================================================
    UICollectionView protocol required methods
    ==========================================================================================
    */

    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return 1
    }

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return tops.count
    }

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("mySingleCell", forIndexPath: indexPath) as! SingleRowCell


        // Display the country name
        if let value = tops[indexPath.row]["topsLabel"] as? String {
            cell.topsLabel.text = value
        }

        // Display "initial" flag image
        var initialThumbnail = UIImage(named: "question")
            cell.topsImageView.image = initialThumbnail

        // Fetch final flag image - if it exists
        if let value = tops[indexPath.row]["tops"] as? PFFile {
            let finalImage = tops[indexPath.row]["tops"] as? PFFile
            finalImage!.getDataInBackgroundWithBlock {
                (imageData: NSData?, error: NSError?) -> Void in
                if error == nil {
                    if let imageData = imageData {
                        cell.topsImageView.image = UIImage(data:imageData)
                    }
                }
            }
        }
        return cell
    }

    /*
    ==========================================================================================
    Segue methods
    ==========================================================================================
    */

    // Process collectionView cell selection
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        let currentObject = tops[indexPath.row]
        performSegueWithIdentifier("CollectionViewToDetailView", sender: currentObject)
    }

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

        // If a cell has been selected within the colleciton view - set currentObjact to selected
        var currentObject : PFObject?
        if let country = sender as? PFObject{
            currentObject = sender as? PFObject
        } else {
            // No cell selected in collectionView - must be a new country record being created
            currentObject = PFObject(className:"Tops")
        }

        // Get a handle on the next story board controller and set the currentObject ready for the viewDidLoad method
        var detailScene = segue.destinationViewController as! DetailViewController
        detailScene.currentObject = (currentObject)
    }


    /*
    ==========================================================================================
    Process Search Bar interaction
    ==========================================================================================
    */

    func searchBarTextDidEndEditing(searchBar: UISearchBar) {

        // Dismiss the keyboard
        searchBar.resignFirstResponder()

        // Reload of table data
        self.loadCollectionViewData()
    }

    func searchBarSearchButtonClicked(searchBar: UISearchBar) {

        // Dismiss the keyboard
        searchBar.resignFirstResponder()

        // Reload of table data
        self.loadCollectionViewData()
    }

    func searchBarCancelButtonClicked(searchBar: UISearchBar) {

        // Clear any search criteria
        searchBar.text = ""

        // Dismiss the keyboard
        searchBar.resignFirstResponder()

        // Reload of table data
        self.loadCollectionViewData()
    }

    /*
    ==========================================================================================
    Process memory issues
    To be completed
    ==========================================================================================
    */

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
}
}

上传图片

  @IBAction func uploadButton(sender: AnyObject) {
        var imageText = uploadMessage.text

        if uploadPreviewImageView.image == nil {
            //image is not included alert user
            println("Image not uploaded")
        }else {

            var posts = PFObject(className: "Tops")
            posts["imageText"] = imageText
            posts["uploader"] = PFUser.currentUser()
            posts.saveInBackgroundWithBlock({
                (success: Bool, error: NSError?) -> Void in

                if error == nil {

                    var imageData = UIImagePNGRepresentation(self.uploadPreviewImageView.image)
                    var parseImageFile = PFFile(name: "uploaded_image.png", data: imageData)
                    posts["imageFile"] = parseImageFile
                    posts.saveInBackgroundWithBlock({
                        (success: Bool, error: NSError?) -> Void in

                        if error == nil {

                            println("finished")
                            self.performSegueWithIdentifier("goHomeFromUpload", sender: self)

                        }else {

                            println(error)
                        }


                    })


                }else {
                    println(error)

                }

0 个答案:

没有答案