简体   繁体   English

永远不会调用Tableview方法

[英]Tableview methods are never called

I have an iOS app where I added a UITableView to my view in Interface Builder. 我有一个iOS应用程序,在其中我向Interface Builder中的视图添加了UITableView Then I set up the tableview in the UIViewController like this: 然后我像这样在UIViewController设置tableview:

tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 50
tableView.delegate = self
tableView.dataSource = self
tableView.contentInset = UIEdgeInsets(top: 25, left: 0, bottom: 0, right: 0)

And I also implemented the following methods: 而且我还实现了以下方法:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    print("test table")
    let course = self.courses[indexPath.row]
    let cell = tableView.dequeueReusableCell(withIdentifier: CourseTableViewCell.identifier, for: indexPath) as! CourseTableViewCell

    cell.make(course, location: self.currentLocation)

    return cell
}

func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return courses.count
}

However, the methods are never called, the table view isn't being displayed and I get no errors in my console? 但是,永远不会调用这些方法,不会显示表视图,并且控制台中没有错误? Is there something I missed while trying to add this? 尝试添加此商品时我错过了什么吗?

Thanks. 谢谢。

As per request in the comments: 根据评论中的要求:

  • The courses array is not empty courses数组不为空
  • The courses array is being filled before reloadData() 将在reloadData()之前填充courses数组

Try this instead of your code : 试试这个代替你的代码:

@IBOutlet weak var tableView: UITableView!{
    didSet {
        self.tableView.delegate = self
        self.tableView.dataSource = self
    }
}

Make sure your controller is conforming to the two protocols: UITableViewDelegate and UITableViewDataSource 确保您的控制器符合以下两个协议: UITableViewDelegateUITableViewDataSource

Then add this method to make sure that your cells do not have 0 pts height: 然后添加此方法以确保您的单元格高度不为0:

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 80 // adjust height of row as needed 
}

just try to implements "heightForRowAtIndexPath" I think your table is properly reloaded but due to constraints in cell UITableViewAutomaticDimension is not working. 只是尝试实现“ heightForRowAtIndexPath”,我认为您的表已正确重新加载,但是由于单元格UITableViewAutomaticDimension中的约束而无法正常工作。 2 - make sure courses is properly allocated, use Debugger in numberOfRowsAtIndexPath to check items in courses 2-确保正确分配课程,在numberOfRowsAtIndexPath中使用Debugger检查课程中的项目

You should test if the delegate methods are executed by setting a breakpoint in a method like numberOfSections or numberOfRowsInSection. 您应该通过在诸如numberOfSections或numberOfRowsInSection之类的方法中设置断点来测试是否执行了委托方法。

When the breakpoint doesn't get hit the delegates are not set properly. 当断点没有命中时,代表的设置不正确。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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