简体   繁体   English

如何修复表视图单元格在启动时不显示?

[英]How to fix Table View Cells not showing on launch?

Greetings fellow reader, to start this all with, I need to say, I have read a lot of similar posts on here with almost the same problem and have implemented a lot of code from here in my project and I can surely say that I am on the same page still, blank table view. 问候各位读者,首先,我要说的是,我在这里阅读过很多类似的文章,几乎都遇到了相同的问题,并且在我的项目中已经从这里实现了很多代码,我可以肯定地说我是在同一页面上,空白表格视图。

I am trying to load data from an API call to the table view cells, primarily only titles (only text) and I am successful with completing the API call and storing the data of the API call into an array which I will use to access data from. 我试图将数据从API调用加载到表视图单元格,主要是仅标题(仅文本),并且我成功完成了API调用并将API调用的数据存储到数组中,以用于访问数据从。

What I have tried: - alternating between fixed and automatic row height - loading up tableView.delegate and tableView.dataSource on viewDidLoad(), also setting the delegate and dataSource in storyboard - alternating between using a forEach() method and using for 'item' in... - setting up breakpoints but I do not get to them at all, for example I do not get to any breakpoints like 'func tableView(_ tableview:...)' nor is the data from those functions printed in console - putting an identifier at the storyboard Table View Cell and using that identifier to print out cells - using .reloadData() in viewWillLoad() 我试过的内容:-在固定行高度和自动行高度之间交替-在viewDidLoad()上加载tableView.delegate和tableView.dataSource,还在情节提要中设置委托和dataSource-在使用forEach()方法和用于'item之间进行交替'in ...-设置断点,但是我根本不了解它们,例如,我没有到达任何类似'func tableView(_ tableview:...)'的断点,这些功能中的数据也不会打印在控制台-在情节提要表View Cell上放置一个标识符,并使用该标识符打印出单元格-在viewWillLoad()中使用.reloadData()

extension HomeViewController: UITableViewDelegate {

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        let item = items[indexPath.row]
        print("Selected Item: \(item)")
    }

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

extension HomeViewController: UITableViewDataSource {

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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        print("CURRENT INDEX PATH BEING CONFIGURED: \(indexPath)")
        let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: ShowsTableViewCell.self), for: indexPath) as! ShowsTableViewCell
            cell.configure(with: items[indexPath.row])

//        let something: Show
//        something = items[indexPath.row]
//
//        cell.titleTVShow.text = something.title
          return cell
    }
}
 switch response.result {
                case .success(let shows):
                    print("Succes: \(shows)")
                    shows.forEach { show in
                        self.items.append(show)
                    }
                case .failure(let error):
                    print("Eror: \(error)")
                }
extension ShowsTableViewCell {
    func configure(with item: Show) {
        titleTVShow.text = item.title
        thumbnailTVShow.image = nil
    }
}
struct Show: Codable {
    let idShow: String
    let title: String
    let imageUrl: String
    let likesCount: Int

    enum CodingKeys: String, CodingKey {
        case idShow = "_id"
        case title
        case imageUrl
        case likesCount
    }
}

I do not get any errors. 我没有任何错误。 The API proceeds with a success and prints out data from the json in console. 该API成功进行,并从控制台中的json打印出数据。 I expect to load the data from the API call in the tableView but that is not happening. 我希望从tableView的API调用中加载数据,但这没有发生。

Try debugging first that if the table view delegate and data source methods are called or not. 首先尝试调试是否调用了表视图委托和数据源方法。 Then reloading the table view after the API response is stored in the array. 然后将API响应存储在数组中后,重新加载表视图。 After this, self.items.append(show). 之后,self.items.append(show)。

Try this. 尝试这个。

 switch response.result {
            case .success(let shows):
                print("Succes: \(shows)")
                shows.forEach { show in
                    self.items.append(show)
                }
                self.tableView.reloadData()
            case .failure(let error):
                print("Eror: \(error)")
            }

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

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