ios - cellForRowAt 索引路径函数未调用

标签 ios swift uitableview

当我尝试运行代码时,cellForRowAtIndexPath 函数由于某种原因无法运行。

numberOfSections 函数运行良好。我认为这是 dataSource/delegate 函数或单元格的问题。

非常感谢找到此问题的解决方案。

import UIKit

class MoviesTableViewController: UITableViewController {
    @IBOutlet weak var searchBar: UISearchBar!


    override func viewDidLoad() {
        super.viewDidLoad()

       tableView.delegate = self
        tableView.dataSource = self


        MovieController.getAllMovies(name: "blue") { (sucess) in
            if sucess {
                DispatchQueue.main.async {
                    self.tableView.reloadData()
                }
            }
        }
        tableView.reloadData()
    }

    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return MovieController.movies.count
    }

    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 148
    }


    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "movieCell", for: indexPath) as! MoviesTableViewCell

        let movie = MovieController.movies[indexPath.row]

        cell.movieRating.text = String(movie.vote_average)
        cell.movieRating.text = movie.overview
        cell.movieTitle.text = movie.title

        return cell
    }

最佳答案

打印调试

override func numberOfSections(in tableView: UITableView) -> Int {
    print("Number of section called")
    return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    print("Number of rows in section called")
    print("Movies count: ", MovieController.movies.count)
    return MovieController.movies.count
}

如果调用的节数未打印到控制台:您的tableView有问题,它是DataSource

else if Movies Count: 0: tableView 数据源运行良好,这是您的 MoviewController.movi​​es 问题。检查您的MovieController

对于下一步,我需要查看 MoviewController 内容。

关于ios - cellForRowAt 索引路径函数未调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50196175/

相关文章:

ios - 为什么我可以在 browserstack 中自动化 .ipa 文件而不是模拟器

swift - 图表未在 tableViewCell 中绘制

ios - 从 iPhone 加载联系人在 Swift 中崩溃

iOS 7 以不同方式布置 accessoryView 和 accessoryType?

ios - 如何删除文件大小小于特定大小(字节)的文件夹内的所有文件

ios - 获取 PHAsset 的 url

xcode - touchesBegan 出现 Swift 错误

ios - 滚动时 UITableView 内容重置

Swift 3 在解包 Optional 值时意外发现 nil

ios - 当应用程序在 ios 9 中处于 InActive 状态时如何处理静默推送通知?