tableview:未调用cellForRowAtIndexPath

时间:2015-11-02 05:25:30

标签: ios tableview swift2 ios9 xcode7

我无法弄清楚tableView: cellForRowAtIndexPath的错误。由于某种原因,它永远不会被召唤。我已经调用了正确的委托和数据源。当我在print("")函数下添加cellForRowAtIndexPath行时,它会在我模拟应用时显示。

先谢谢你。

这是我整个页面的代码:

class MainPageViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

var sportCells = [PFObject]()


@IBOutlet weak var tableView: UITableView!

@IBOutlet weak var profilePictureImageView: UIImageView!
@IBOutlet weak var fullNameLabel: UILabel!





override func viewDidLoad() {
    super.viewDidLoad()

    self.tableView.dataSource = self
    self.tableView.delegate = self

    //Do any additional setup after loading the view.
}
override func viewDidAppear(animated: Bool) {
    updateSportsTable()
    print("its happening")
    let lastName = PFUser.currentUser()! ["last_name"]
    if let firstName = PFUser.currentUser()?["first_name"] as? String {
        self.fullNameLabel.text = "\(firstName) \(lastName)"


    }

    if let userPicture = PFUser.currentUser()?["profile_picture"] as? PFFile {
        userPicture.getDataInBackgroundWithBlock { (imageData: NSData?, error: NSError?) -> Void in
            if (error == nil) {
                self.profilePictureImageView.image = UIImage(data:imageData!)


            }
        }
    }
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    print("sportCells count is \(sportCells.count)")
    return sportCells.count

}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    print("data extracted1")
    let cell = tableView.dequeueReusableCellWithIdentifier("sportCell") as! SportTableViewCell
   print("data extracted")
    let sportPost = self.sportCells[indexPath.row]
    let user = sportPost["user"] as! PFUser
    print("data extracted")
    do {
        try user.fetchIfNeeded()
        print("its happening 3rd time")
    } catch _ {
        print("There was an error")
    }

    cell.sportTitle.text = sportPost["basketballTitle"] as? String
    cell.sportLogo.text = sportPost["basketballLogo"] as? String
    cell.numberOfPOTM.text = "5"
    return cell
}



func updateSportsTable() {
    let query = PFQuery(className: "Sports")
    query.findObjectsInBackgroundWithBlock { (sportCells:[PFObject]?, error:NSError?) -> Void in
        if error == nil {
            self.tableView.reloadData()
            print("its happening again")
        }


    }



}

1 个答案:

答案 0 :(得分:0)

从评论中确认,问题是模型sportCells中的数据填充。

确保正确填充sportCells并在此之后致电self.tableView.reloadData