Ios tableview:未调用cellForRowAtIndexPath
我无法找出我的Ios tableview:未调用cellForRowAtIndexPath,ios,tableview,swift2,ios9,xcode7,Ios,Tableview,Swift2,Ios9,Xcode7,我无法找出我的表格视图:cellforrowatinexpath有什么问题。它永远不会因为某种原因被呼叫。我已经调用了适当的委托和数据源。当我在cellforrowatinexpath函数下添加print(“”)行时,它在模拟应用程序时从未出现过 提前谢谢你 以下是我的整个页面代码: class MainPageViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { var sportCell
表格视图:cellforrowatinexpath
有什么问题。它永远不会因为某种原因被呼叫。我已经调用了适当的委托和数据源。当我在cellforrowatinexpath
函数下添加print(“”)行时,它在模拟应用程序时从未出现过
提前谢谢你
以下是我的整个页面代码:
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")
}
}
}
从评论中确认,您的问题在于型号sportCells
中的数据填充
确保正确填充了sportCells
,并在此之后调用了self.tableView.reloadData
。从评论中确认,问题在于型号sportCells
中的数据填充
确保正确填充了sportCells
,然后调用self.tableView.reloadData
。在numberOfRowsInSection
中打印的sportCells.count
是什么?此外,如果要在序列图像板或XIB中添加表视图,请尝试删除约束并重新添加。sportCells.count打印0。我想这是因为没有调用CellForRowatineXpath。我会尝试删除这些约束,然后重新添加它们,看看会发生什么,然后再回复您。谢谢sportCells.count==0
->您的问题。问题在于方法updateSportsTable()。由于您没有将数据保存在名为“sportCells”的数组中,所以您可以开始了sportCells.count
必须为您的cellforrowatinexpath
返回正值才能被调用。确保正确填充sportCells
,并调用self.tableView.reloadData
发布。在numberOfRowsInSection
中打印的sportCells.count
是什么?此外,如果要在序列图像板或XIB中添加表视图,请尝试删除约束并重新添加。sportCells.count打印0。我想这是因为没有调用CellForRowatineXpath。我会尝试删除这些约束,然后重新添加它们,看看会发生什么,然后再回复您。谢谢sportCells.count==0
->您的问题。问题在于方法updateSportsTable()。由于您没有将数据保存在名为“sportCells”的数组中,所以您可以开始了sportCells.count
必须为您的cellforrowatinexpath
返回正值才能被调用。确保正确填充了sportCells
,并且调用了self.tableView.reloadData
post。如果sportCells在cellForRowAtIndexPath下填充,该如何填充?我是否遗漏了一个简单的想法…我想我的问题是这行代码:query.findobjectsinbackgroundithblock{(sportCells:[PFObject]?,错误:NSError?->Void in…..如果我尝试使用[AnyObject]代替PFObject,Xcode崩溃,所有代码文本变为黑色…您从未在CellForRowatineXpath
中加载模型。在viewDidLoad
中加载数据,然后调用self.tableView.reloadData
。如果在CellForRowatineXpath下填充sportCells,该如何填充它?我是否缺少一个简单的示例这里的想法…我想我的问题是这行代码:query.findobjectsinbackgroundithblock{(sportCells:[PFObject]?,error:NSError?->Void in.…如果我尝试使用[AnyObject]代替PFObject,Xcode崩溃,所有代码文本变为黑色…您永远不会在cellforrowatinexpath
中加载模型。在viewDidLoad
中加载数据,然后调用self.tableView.reloadData
。