ios - TableView (_ : cellForRowAtIndexPath) is not called and TableView doesn't show any data

标签 ios swift xcode xcode9.1

我放置了一个 TableView Controller 并嵌入到 IB 上的导航 Controller 中。之后,我使用以下代码创建一个 WalletsTableViewController 类:

class WalletsTableViewController: UITableViewController {

    private var wallets = [Wallet]()

    override func viewDidLoad() {
        super.viewDidLoad()
        fetchWallets()
    }

    private func fetchWallets(){
        let managedObjectContext = AppDelegate.managedContext
        let fetchRequest: NSFetchRequest<Wallet> = Wallet.fetchRequest()
        do {
            wallets = try managedObjectContext.fetch(fetchRequest)
        } catch {
            print(error.localizedDescription)
        }
    }

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

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "WalletCell", for: indexPath) 
        let wallet = wallets[indexPath.row]
        cell.textLabel?.text = wallet.name
        return cell
    }
}

我实现了一个持久性应用程序,这就是为什么我有 fetchWallets() 方法并且它运行良好。我可以将数据打印到控制台,但 tableView 不想显示我的元素。此外,不会调用已实现的 tableView() 方法。 我该如何解决这个问题?

编辑: tableView.reloadData() 没有帮助

编辑2:这是我的 Storyboard: storyboard

最佳答案

按照上面评论中的建议,尝试以下操作:

    private func fetchWallets(){
    let managedObjectContext = AppDelegate.managedContext
    let fetchRequest: NSFetchRequest<Wallet> = Wallet.fetchRequest()
    do {
        wallets = try managedObjectContext.fetch(fetchRequest)
        self.tableView.reloadData()
    } catch {
        print(error.localizedDescription)
    }
}

关于ios - TableView (_ : cellForRowAtIndexPath) is not called and TableView doesn't show any data,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47532292/

相关文章:

ios - NSURLConnection 超时后卡住

ios - StackView 的水平分布不起作用

ios - 为什么我无法在 xcode 中选择 segue 类型?

swift - 如何快速将文档保存到 "Files"App

ios - 隐藏导航栏 ios swift 生命周期方法未调用导致问题

ios - 以 NSException 类型的未捕获异常终止 - 删除 TableView 行

iOS - Storyboard - 无法查看嵌入在 ScrollView 中

ios - 如何将 UITextView 的边框设置为与 UITextField 的边框颜色相同

ios - 无法导入 Alamofire

ios - 将数据发送到嵌入 UISplitViewController 内的容器内的 Tableview