ios - 每个部分应该返回多少行?

标签 ios swift uitableview parse-platform pfquerytableviewcontrolle

我正在尝试创建一个类似于 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)")
}

最佳答案

您要覆盖部分中的行数以返回用户计数,但这始终为零,因为您从未向字典添加任何内容。

PF 查询 TableView Controller 的重点在于它为您管理数据收集,但您正在替换其中的关键部分并破坏系统。返回并检查用户指南,以确定您的子类需要如何工作才能达到您想要的效果。

关于ios - 每个部分应该返回多少行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33030131/

相关文章:

ios - UIGestureRecognizerDelegate 来管理手势——我可以解除触摸分配吗?

ios - UITableViewController : Update cell from DOWNLOADING to DONE

ios - 设置单元格高度取决于嵌入的collectionview的内容

ios - Swift 2.3 到 Swift 3.1 迁移 - Alamofire 请求错误

iOS Restkit 需要调用NewBodyStream

android - 弹跳按钮 flutter

swift - 如何在不单击复选框的情况下快速更改复选框的状态?

ios - UITableviewCell - 在编辑模式下单击空复选标记圆圈不会选择单元格

ios - 当使用 lineHeightMultiple 控制行间行距时,如何在 UITableViewCell 中垂直对齐我的 UILabel?

ios - 滚动后 API 回调中 Tableview 单元格数据发生更改