我正在尝试创建一个类似于 Yik Yak 的查询类型,我可以在其中看到某个位置内的消息查询,但是每当我发布消息时,结果都不会在查询中显示数据。我基本上希望我发送的消息显示在查询中。我想我在代码中正确地做了我的逻辑,但我在这里遗漏了一些可以在查询中显示数据的东西。上周我一直在解决这个问题,仅此一个修复程序就可以结束我项目的这一部分。谁能帮我这个?
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)")
}