【发布时间】:2015-02-03 01:04:38
【问题描述】:
我正在开发一个应用程序,我希望它能够根据与当前用户相距一定距离的用户填充单元格。由于某种原因,customcells 没有填充正确的对象。应该检索的标签和图像是空白的。我得到的只是一个空白单元格。我确保我给单元格标识符提供了正确的名称,并且我还确保将 tableviewcontroller 和 tablecellview 链接到它们各自的类,但仍然没有运气。
首先我创建了初始化器:
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
}
最后我退回了单元格,但由于某种原因它不起作用:
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
}
ps,我将节数设置为 1,只是不认为该方法在这里显示有用。
【问题讨论】:
-
我尝试使用 cellforrowatindexpath 中的 println() 检查 cell.userName 是否为零,但似乎由于某种原因没有调用该方法....
-
对象是字典。当我尝试使用 println 中的键检索值时,不会调用 println 函数,并且由于它位于 cellforrowatindex 路径中,我们可以假设该函数也没有被调用
-
你能展示你的
tableview numberOfRowsInSection函数吗?您是否在cellForRowAtIndexPath内调用objectAtIndexPath来获取每一行的数据?下载数据后你尝试调用 tableView.reloadData() 吗?
标签: xcode swift parse-platform