我试图创建一个页面来显示用户在表格中关注的对象。一切似乎都很清楚,但由于某些原因,这段代码没有加载我的数据。以下是我的以下页面控制器:
导入基金会 import Parse
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: {});
}
}
我为我的单元格创建了一个自定义类,并将其称为FollowCell。所有出口都连接到类中的变量以及表数据源&委托给ViewController!如果您发现可能导致此表不显示数据的任何内容,请告诉我。感谢。
编辑:发现问题是即使我在for循环中为每个用户加载resultData数组,在findObjectInBackgroundWithBlock调用之外,resultData计数返回0.所以它就像它不是实际上更新了数组。有人知道为什么会这样吗?
答案 0 :(得分:2)
您忘了重新加载tableview
mytableview.reloadData()
每次加载数据时都需要重新加载完整的表视图或将单元格附加到它...
并检查在storyboard中正确声明的单元格标识符
答案 1 :(得分:0)
您可能忘记为故事板中的Cell设置标识符" FollowCell",仔细检查它。还要重新加载tableView。
答案 2 :(得分:0)
我发现了问题,当我在第二个查询之外重新加载数据时出现问题。重新加载第二个查询调用内部和内部调用之外的数据!