简体   繁体   中英

tableview: cellForRowAtIndexPath is not being called

I can't figure out what is wrong with my tableView: cellForRowAtIndexPath . its never getting called for some reason. i have called the proper delegate and datasource. When I add a print("") line under the cellForRowAtIndexPath function, it never appears when i simulate the app.

Thank you in advanced.

here is my code for the whole page:

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")
        }


    }



}

As confirmed from the comments, you problem is data population in model sportCells .

Ensure that sportCells is populated properly and that you call self.tableView.reloadData after that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM