2015-01-31 51 views
1

我創建了一個tableViewcontroller,併爲它分配了故事板中的自定義類:PFQueryTableViewController。然後,我也給了它parseClassName "userMessage",由於某種原因,當我嘗試運行應用程序時,我總是得到相同的錯誤信息:NSInternalInconsistencyException', reason: 'You need to specify a parseClassName for the PFQueryTableViewController. 我不明白爲什麼我得到這個錯誤,因爲我明確地給這個類一個parseClassName。您需要爲PFQueryTableViewController指定一個parseClassName

這是我的相關聯的PFQueryTabletableViewController代碼:

import UIKit import CoreLocation import Parse

class TableViewController: PFQueryTableViewController, CLLocationManagerDelegate {

let userMessages = ["blah blahh blahhh", "Beep Beep Boop", "Beep Beep Bobbity boop"] 

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 = "userMessage" 
    self.textKey = "text" 
    self.pullToRefreshEnabled = true 
    self.objectsPerPage = 40 

} 





private func alert(message: String){ 
    let alert = UIAlertController(title: "Uh-OH", 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 = 120 
    self.tableView.rowHeight = 120 
    locationManager.desiredAccuracy = 100 
    locationManager.delegate = self 
    locationManager.requestWhenInUseAuthorization() 
    locationManager.startUpdatingLocation() 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
} 

func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) { 
    alert("Cannot fetch your location!!") 
} 

override func queryForTable() -> PFQuery! { 
    let query = PFQuery(className: "Messages") 
    if let queryLoc = currLocation { 
     query.whereKey("location", nearGeoPoint: PFGeoPoint(latitude: queryLoc.latitude, longitude: queryLoc.longitude), withinMiles: 1) 
     query.limit = 40 
     query.orderByDescending("createdAt") 
    }else { 
     query.whereKey("location", nearGeoPoint: PFGeoPoint(latitude: 37.41182, longitude: -121.941125), withinMiles: 1) 
     query.limit = 40 
     query.orderByDescending("createdAt") 
    } 

    return query 
} 

func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) { 
    locationManager.stopUpdatingLocation() 
    if(locations.count > 0) { 
     let location = locations[0] as CLLocation 
     println(location.coordinate) 
     currLocation = location.coordinate 
    } else { 
     alert("Cannot fetch your loation") 
    } 
} 

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 numberOfSectionsInTableView(tableView: UITableView) -> Int { 
    return 1 
} 

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return userMessages.count 
} 

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath!, object: PFObject!) -> PFTableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as TableViewCell 
    cell.messageText.text = object.valueForKey("text") as? String 
    cell.messageText.numberOfLines = 0 
    let views = object.valueForKey("count") as Int 
    cell.numberOfViewsLabel.text = "\(views)" 
    cell.numberOfViewsLabel.text = "\((indexPath.row + 1) * 5)" 
    return cell 
} 

func addToViews(sender: AnyObject) { 
    let hitPoint = sender.convertPoint(CGPointZero, toView: self.tableView) 
    let hitIndex = self.tableView.indexPathForRowAtPoint(hitPoint) 
    let object = objectAtIndexPath(hitIndex) 
    object.incrementKey("count") 
    object.saveInBackgroundWithBlock { (Bool, NSError) -> Void in 
     //blahhh 
    } 
    self.tableView.reloadData() 

} 

} `

回答

0

parseClassName是隻讀變量和子類時才使用PFObject。 https://parse.com/docs/ios/api/Classes/PFObject.html#//api/name/parseClassName

對象的類名。

@property(強,只讀)的NSString * parseClassName

宣佈 PFObject.h

對象 -

@implementation MYGame 

@dynamic title; 

+ (NSString *)parseClassName { 
    return @"Game"; 
} 

@end 

斯威夫特

class MYGame: PFObject { 
    class func parseClassName() -> String! { 
     return "Game" 
    } 
} 
0

在我我曾使用過的情況d一個故事板,需要在我的PFQueryTableViewController子類中創建一個initWithCoder:方法。模板指出,在Parse.com文檔缺乏這種方法,但下面的例子中,第一個註釋不包括的範例:https://gist.github.com/jamesyu/ba03c1a550f14f88f95d#gistcomment-74202

消息「你需要指定一個parseClassName爲PFQueryTableViewController」正在生成因爲沒有任何方法正在設置PFQueryTableViewControllerparseClassName屬性。您會注意到,文檔中的initWithStyle:方法示例中的被明確定義。但是,如果視圖是通過故事板加載的,則該方法將不會被調用:爲此,您需要在initWithCoder:方法中設置parseClassName

另外,不要混淆PFQueryTableViewController的子類爲PFObject。對於PFObject,您需要創建一個名爲parseClassName的類方法,並在調用[解析setApplicationId:aid clientKey:ckey]之前註冊該子類。您不會爲PFQueryTableViewController或任何其他ParseUI視圖控制器執行這些操作。他們依靠一個或多個init方法。

相關問題