我正在尝试创建一个页面,以在表格中显示用户正在关注的用户。对我来说,一切似乎都很清楚,但由于某些原因,这段代码没有加载我的数据。下面是我的页面控制器:
导入基础导入解析
class FollowingController: UIViewController, UITableViewDataSource, UITableViewDelegate{
var profileId : String = String()
@IBOutlet weak var loadingIndicator: UIActivityIndicatorView!
@IBOutlet weak var topTitleBar: UIImageView!
@IBOutlet weak var mainBox: UIImageView!
@IBOutlet var tableView: UITableView!
var resultData:NSMutableArray = NSMutableArray()
func loadData(){
resultData.removeAllObjects()
loadingIndicator.hidden = false
loadingIndicator.startAnimating()
var counter = 0
let followingQuery = PFQuery(className: "Followers")
followingQuery.whereKey("following", equalTo: profileId)
followingQuery.findObjectsInBackgroundWithBlock { (objects, error) -> Void in
if let objects = objects {
for object in objects {
let userQuery = PFUser.query()
userQuery?.whereKey("objectId", equalTo: object["followed"] as! String)
userQuery?.findObjectsInBackgroundWithBlock({ (users, error ) -> Void in
if let users = users {
for user in users{
let followed = user as! PFUser
self.resultData.addObject(followed)
counter++
}
}
})
}
}
let array:NSArray = self.resultData.reverseObjectEnumerator().allObjects
self.resultData = NSMutableArray(array: array)
self.tableView.reloadData()
if counter == 0 {
//Handle no results
}
}
loadingIndicator.hidden = true
}
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidAppear(animated: Bool) {
self.loadData()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return resultData.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell:FollowingCell = tableView.dequeueReusableCellWithIdentifier("FollowingCell", forIndexPath: indexPath) as! FollowingCell
let user:PFUser = self.resultData.objectAtIndex(indexPath.row) as! PFUser
let name = (user.objectForKey("firstName") as! String) + " " + (user.objectForKey("lastName") as! String)
cell.name.text = name
if let profilePicture = user.objectForKey("profilePicture") as? PFFile {
profilePicture.getDataInBackgroundWithBlock({ (data, error) -> Void in
if error == nil {
let profileImage = UIImage(data: data!)
cell.profilePicture.image = profileImage
}
})
}
return cell
}
override func viewDidLayoutSubviews() {
}
@IBAction func backButtonPressed(sender: AnyObject) {
self.dismissViewControllerAnimated(true, completion: {});
}
}我为我的单元创建了一个自定义类,并将其命名为FollowingCell。所有的出口都连接到类中的变量以及表数据源&委托给ViewController!如果您发现了任何可能导致此表无法显示数据的原因,请让我知道。谢谢。
编辑:发现问题在于,即使我在for循环中为每个用户加载了resultData数组,在findObjectInBackgroundWithBlock调用之外,resultData计数仍返回0。这就好像它实际上并没有更新数组。有人知道为什么会这样吗?
发布于 2016-03-06 14:04:55
您忘记了重新加载表视图
mytableview.reloadData()每次加载数据时,都必须重新加载整个表视图或向其追加单元格。
并检查序列图像板中正确声明的单元格标识符
发布于 2016-03-06 14:09:28
你可能忘了在故事板中为你的单元格设置标识符为"FollowingCell",仔细检查一下。还要重新加载tableView。
发布于 2016-03-06 15:09:59
我发现了问题,当我在第二个查询之外重新加载数据时出现了问题。在第二个查询调用中重新加载数据,而在内部调用之外重新加载数据!
https://stackoverflow.com/questions/35823703
复制相似问题