0

I'm trying to create a query sort of like Yik Yak where I can see a query of messages within a certain location but the results are not displaying the data in the query whenever I post a message. I basically want the message I send to be shown in the query. I think I did my logic correctly in the code but I'm missing something here that could show the data in the query. I've been working on this problem for the last week and just this one fix could end this segment of my project. Can anyone help me with this?

import UIKit
import ParseUI
import Parse
import CoreLocation

@available(iOS 8.0, *)
class HomeViewController: PFQueryTableViewController,CLLocationManagerDelegate {

var messages = [String]()
var users = [String: String]()

let bubbleFeeds = [
    ("1"),
    ("2"),
    ("I3"),
    ("4"),
    ("5"),
    ("6") ]

let locationManager = CLLocationManager()
var currLocation : CLLocationCoordinate2D?

override init(style: UITableViewStyle, className: String!) {
    super.init(style: style, className: className)
}


required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)

    self.parseClassName = "BubbleTest"
    self.textKey = "textField"
    self.pullToRefreshEnabled = true
    self.objectsPerPage = 200

}

private func alert(message : String) {
    let alert = UIAlertController(title: "Oops something went wrong.", message: message, preferredStyle: UIAlertControllerStyle.Alert)
    let action = UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil)
    let cancel = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil)
    let settings = UIAlertAction(title: "Settings", style: UIAlertActionStyle.Default) { (action) -> Void in
        UIApplication.sharedApplication().openURL(NSURL(string: UIApplicationOpenSettingsURLString)!)
        return
    }
    alert.addAction(settings)
    alert.addAction(action)
    self.presentViewController(alert, animated: true, completion: nil)
}



override func viewDidLoad() {
    super.viewDidLoad()
    self.tableView.estimatedRowHeight = 60
    self.tableView.rowHeight = UITableViewAutomaticDimension
    locationManager.desiredAccuracy = 1000
    locationManager.delegate = self
    locationManager.requestWhenInUseAuthorization()
    locationManager.startUpdatingLocation()


    // 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()
}

func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
    alert("Cannot fetch your location")
}




func queryForTable() -> PFQuery! {
    let query = PFQuery(className: "BubbleTest")
    if let queryLoc = currLocation {
        query.whereKey("location", nearGeoPoint: PFGeoPoint(latitude: queryLoc.latitude, longitude: queryLoc.longitude), withinMiles: 10)
        query.limit = 200;
        query.orderByDescending("createdAt")
    } else {
        /* Decide on how the application should react if there is no location available */
        query.whereKey("location", nearGeoPoint: PFGeoPoint(latitude: 37.411822, longitude: -121.941125), withinMiles: 10)
        query.limit = 200;
        query.orderByDescending("createdAt")
    }

    return query
}


func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    locationManager.stopUpdatingLocation()
    if(locations.count > 0){
        let location = locations[0]
        print(location.coordinate)
        currLocation = location.coordinate
    } else {
        alert("Cannot receive your location")
    }
}


    override func objectAtIndexPath(indexPath: NSIndexPath!) -> PFObject! {
        var obj : PFObject? = nil
        if(indexPath.row < self.objects!.count){
            obj = self.objects![indexPath.row] as! PFObject
        }

        return obj
    }


//    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
//
//




// MARK: - Table view data source

override func numberOfSectionsInTableView(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 users.count

}



override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("object", forIndexPath: indexPath) as! Bubbles
let object = PFObject(className: "BubbleTest")

cell.name.text = object.valueForKey ("userName") as? String
cell.message.text = object.valueForKey("textField") as? String
cell.dateTime.text = "\((indexPath.row + 1) * 3)m ago"
cell.message.numberOfLines = 0
let score = object.valueForKey("count") as! Int
cell.likeCount.text = "\(score)"
let replycnt = object.valueForKey("replies") as! Int
cell.responseCount.text = "\(replycnt) replies"
//cell.userImage.image = PFUser.currentUser()?.valueForKey("photo") as! PFFile


// Configure the cell...

return cell
}

@IBAction func likeButton(sender: AnyObject) {
            let hitPoint = sender.convertPoint(CGPointZero, toView: self.tableView)
            let hitIndex = self.tableView.indexPathForRowAtPoint(hitPoint)
            let object = objectAtIndexPath(hitIndex)
            object.incrementKey("count")
            object.saveInBackground()
            self.tableView.reloadData()
            NSLog("Top Index Path \(hitIndex?.row)")
}

1 Answers1

0

You're overriding number of rows in section to return the count of users, but that is always zero because you never add anything to the dictionary.

The whole point of a PF query table view controller is that it manages the data collection for you, but you're replacing key parts of that and breaking the system. Go back and check the user guide to decide how your subclass needs to work for the effect you want.

Wain
  • 118,658
  • 15
  • 128
  • 151
  • what do you mean by user guide? Also I've returned the number of rows in section to zero but I still see empty rows and lines with none of the data being displayed in the query – JoshyBroheme Oct 11 '15 at 02:04
  • parse provides a lot of user guides related to their APIs and SDKs, they cover information on customising and using their table view controller. take out all your code which changes row counts and get a basic table working first using the superclass logic, then customise from there... – Wain Oct 11 '15 at 08:30
  • I checked the user guide for PFTableViewControllers and it pretty much confirmed that it doesn't need a cellforRowAtIndexPath. So I created a new file and added my code without the row counts this time but I'm not able to see the data displayed on the query. I'm not sure if I did correctly what was told but is there something else that needs to be done? – JoshyBroheme Oct 12 '15 at 22:37
  • Supply the query and the key to display on the cell as minimum. Check objects are loaded. That should give you a simple list you can build on. – Wain Oct 13 '15 at 06:38
  • Sorry I'm really new to coding so what do you mean by suppling the query and key? – JoshyBroheme Oct 13 '15 at 17:09
  • The configuration required for the query VC to show anything. Get the simplest thing possible working first – Wain Oct 13 '15 at 19:22