Xcode PFQueryTableView控制器未加载自定义单元格
我正在开发一个应用程序,我希望它能够根据与当前用户在设定距离内的用户填充单元格。由于某些原因,CustomCell没有使用正确的对象填充。应该检索的标签和图像为空。我得到的只是一个空白的手机。我确保给了单元格标识符正确的名称,并且确保将tableviewcontroller和tablecellview链接到它们各自的类,但仍然没有成功 首先,我创建了初始值设定项:Xcode PFQueryTableView控制器未加载自定义单元格,xcode,swift,parse-platform,Xcode,Swift,Parse Platform,我正在开发一个应用程序,我希望它能够根据与当前用户在设定距离内的用户填充单元格。由于某些原因,CustomCell没有使用正确的对象填充。应该检索的标签和图像为空。我得到的只是一个空白的手机。我确保给了单元格标识符正确的名称,并且确保将tableviewcontroller和tablecellview链接到它们各自的类,但仍然没有成功 首先,我创建了初始值设定项: class TableViewController: PFQueryTableViewController, CLLocationM
class TableViewController: PFQueryTableViewController, CLLocationManagerDelegate {
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 = "User"
self.textKey = "FBName"
// self.imageKey = "pictureURL"
self.pullToRefreshEnabled = true
self.objectsPerPage = 10
self.paginationEnabled = true
}
然后在viewDidLoad中,我启用了位置服务:
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.estimatedRowHeight = 200
self.locationManager.requestWhenInUseAuthorization()
if CLLocationManager.locationServicesEnabled() {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestAlwaysAuthorization()
locationManager.startUpdatingLocation()
loadData()
println("location services enabled bruh")
}
}
接下来,我重写queryfortable函数:
override func queryForTable() -> PFQuery! {
let query = PFQuery(className: "User")
if let queryLoc = currLocation {
query.whereKey("location", nearGeoPoint: PFGeoPoint(latitude: queryLoc.latitude, longitude: queryLoc.longitude), withinMiles: 50)
query.limit = 40
query.orderByAscending("createdAt")
println("\(queryLoc.latitude)")
return query
} else {
query.whereKey("location", nearGeoPoint: PFGeoPoint(latitude: 37.411822, longitude: -121.941125), withinMiles: 50)
query.limit = 40
query.orderByAscending("createdAt")
println("else statement")
return query
}
}
然后是objectAtIndexPath函数
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
}
ps,我将节数设置为1,只是我不认为在这里显示该方法有用。好的,我发现了问题!问题是我试图使用
PFQuery
来检索PFUser
的列表。我发现,使用PFQuery
infactPFUser
无法做到这一点,因为它有自己的查询方法从用户那里检索信息
我所要做的就是更换这一行:
let query=PFQuery(类名:“用户”)
为此:
let query=PFUser.query()
我试图从CellForRowatineXpath中使用println()检查它的cell.userName是否为nil,但似乎由于某种原因没有调用该方法……该对象是一个字典。当我尝试使用println中的键检索值时,println函数未被调用,并且由于它位于ing cell for rowatinex path中,我们可以假设该函数未被调用。您是否可以显示tableview numberOfRowsInSection
函数?您是否在cellforrowatinexpath
内部调用objectAtIndexPath
以获取每一行的数据?下载数据后是否尝试调用tableView.reloadData()?
override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!, object: PFObject?) -> PFTableViewCell! {
let cell = tableView.dequeueReusableCellWithIdentifier("CustomCell", forIndexPath: indexPath) as TableViewCell
cell.userName?.text = object?.valueForKey("FBName") as? String
let userProfilePhotoURLString = object?.valueForKey("pictureURL") as? String
var pictureURL: NSURL = NSURL(string: userProfilePhotoURLString!)!
var urlRequest: NSURLRequest = NSURLRequest(URL: pictureURL)
NSURLConnection.sendAsynchronousRequest(urlRequest, queue: NSOperationQueue.mainQueue()) { (NSURLResponse response, NSData data,NSError error) -> Void in
if error == nil && data != nil {
cell.userImage?.image = UIImage(data: data)
}
}
cell.ratingsView?.show(rating: 4.0, text: nil)
return cell
}