当我尝试运行代码时,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.movies
问题。检查您的MovieController
对于下一步,我需要查看 MoviewController
内容。
关于ios - cellForRowAt 索引路径函数未调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50196175/