繁体   English   中英

永远不会调用Tableview方法

[英]Tableview methods are never called

我有一个iOS应用程序,在其中我向Interface Builder中的视图添加了UITableView 然后我像这样在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)

而且我还实现了以下方法:

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
}

但是,永远不会调用这些方法,不会显示表视图,并且控制台中没有错误? 尝试添加此商品时我错过了什么吗?

谢谢。

根据评论中的要求:

  • courses数组不为空
  • 将在reloadData()之前填充courses数组

试试这个代替你的代码:

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

确保您的控制器符合以下两个协议: UITableViewDelegateUITableViewDataSource

然后添加此方法以确保您的单元格高度不为0:

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

只是尝试实现“ heightForRowAtIndexPath”,我认为您的表已正确重新加载,但是由于单元格UITableViewAutomaticDimension中的约束而无法正常工作。 2-确保正确分配课程,在numberOfRowsAtIndexPath中使用Debugger检查课程中的项目

您应该通过在诸如numberOfSections或numberOfRowsInSection之类的方法中设置断点来测试是否执行了委托方法。

当断点没有命中时,代表的设置不正确。

暂无
暂无

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

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