“_”类型的值没有成员 'count' 和 'indexPath'

Value of type '_' has no member 'count' and 'indexPath'

各位!我在连接 JSON 和 table 视图时遇到问题。函数中有两个错误:

Value of type 'BalanceStruct?' has no member 'count'

Value of type 'BalanceStruct?' has no member 'indexPath'

import UIKit

class BalanceViewController: UIViewController, UITableViewDelegate, UITableViewDataSource  {
    
    @IBOutlet weak var tableView: UITableView!
    
    
    var balances: BalanceStruct?
    
    override func viewDidLoad() {
        super.viewDidLoad()

        ServerManager.shared.getBalanceList(token: UserDefaults.standard.value(forKey: "token") as! String, { (balanceList) in
            self.balances = balanceList
            self.tableView.delegate = self
            self.tableView.dataSource = self
            self.tableView.reloadData()
        })
        {(error) in print(error)}

    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return balances.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let balance = balances.indexPath.row
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "BalanceCell", for: indexPath) as! BalancesViewCell
        cell.configure(balances: balance)
        return cell
    }
    
}

问题出在这部分:

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

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let balance = balances.indexPath.row

    let cell = tableView.dequeueReusableCell(withIdentifier: "BalanceCell", for: indexPath) as! BalancesViewCell
    cell.configure(balances: balance)
    return cell
}

BalanceStruct 是一个模型

struct BalanceStruct: Codable {
    let content: [ContentBS]
    let pageable: Pageable
    let totalPages, totalElements: Int
    let last: Bool
    let sort: Sort
    let numberOfElements: Int
    let first: Bool
    let size, number: Int
    let empty: Bool
}

// MARK: - Content
struct ContentBS: Codable {
    let id: Int
    let dateCreated: String
    let dateUpdated: String?
    let name: String
    let balance: Int
}

我该如何解决?谢谢!

table 视图数据源必须是 数组(或至少是集合类型)

var balances = [BalanceStruct]()

那么数据源方法是

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

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let balance = balances[indexPath.row] // must be index subscription

    let cell = tableView.dequeueReusableCell(withIdentifier: "BalanceCell", for: indexPath) as! BalancesViewCell
    cell.configure(balances: balance)
    return cell
}

但是如果 content 数组应该被显示替换

var balances = [BalanceStruct]()

var contentBS = [ContentBS]()

self.balances = balanceList

self.contentBS = balanceList.content

现在数据源方法是

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

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let balance = contentBS[indexPath.row]

    let cell = tableView.dequeueReusableCell(withIdentifier: "BalanceCell", for: indexPath) as! BalancesViewCell
    cell.configure(balances: balance)
    return cell
}