TableViewCell与JSON Swift的模型

时间:2018-06-13 14:33:09

标签: json swift uitableview uisegmentedcontrol

由于未捕获的异常终止应用程序' NSInternalInconsistencyException',原因:'无法在捆绑中加载NIB:' NSBundle(已加载)'名称' customCell''

我有一个Json:

{
"organizations": [
    {
      "title": "Universal Bank",
        "cityId": "7oiylpmiow8iy1smadi",
        "currencies": {
            "EUR": {
                "ask": "31.0500",
                "bid": "30.6500"
            },
            "USD": {
                "ask": "26.1300",
                "bid": "26.0000"
            }
        }
    },

请帮我在ViewController中进行正确的SegmentControl(按行号和UITableCell分段显示货币 - >交换)。这是我的UITableViewCell:

class tableViewCell: UITableViewCell {
@IBOutlet weak var nameCurrency: UILabel!
@IBOutlet weak var exchangeRates: UILabel!
override func awakeFromNib() {
    super.awakeFromNib()
}
  func configure(exchangeRate: Bank, index: Int) {
self.nameCurrency.text = exchangeRate.rates[index].name
self.exchangeRates.text = " \(exchangeRate.rates[index].ask)  / \(exchangeRate.rates[index].bid)"
}
}

ViewController和已注册的Cell:

class ExchangeViewController: UIViewController, UITableViewDelegate, UIScrollViewDelegate, UITableViewDataSource {
var zxc: Int!
private struct Constants {
    static let exchangeTableViewCellId = "customCell"
}
 var ratesSectionsArray = [Bank]()
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var segLabel: UISegmentedControl!
@IBAction func switchCustomBank(_ sender: UISegmentedControl) {
    switch segLabel.selectedSegmentIndex {
    case 0:
    case 1:
    case 2:
    case 3:
    default:
    }
override func viewDidLoad() {
    super.viewDidLoad()

    let nib = UINib(nibName: Constants.exchangeTableViewCellId, bundle: nil)
    tableView.register(nib, forCellReuseIdentifier: Constants.exchangeTableViewCellId)
    zxc = 0
   let baseUrlString = "http://resources.finance.ua/ru/public/currency-cash.json"
    guard let url = URL(string: baseUrlString) else { return }
    URLSession.shared.dataTask(with: url) {
        (data, response, error) in
        if let data = data {
            do{
                let json = try JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.allowFragments)
                guard let jsonDict = json as? [String: Any],
                    let organizationsArray = jsonDict["organizations"] as? [[String: Any]]
                else {return}
                for organization in organizationsArray{
                   guard let bank = Bank(json: organization)
                        else { continue }
                    self.ratesSectionsArray.append(bank)
                }
                DispatchQueue.main.async {
                    self.tableView.reloadData()
                }
            }
            catch{
                print("error")
            }
        }
    }.resume()
    }
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return ratesSectionsArray.count
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: Constants.exchangeTableViewCellId, for: indexPath) as! tableViewCell
    let exchangesRates = ratesSectionsArray[indexPath.row]
    cell.configure(exchangeRate: exchangesRates, index: indexPath.row)
    return cell
}
}

0 个答案:

没有答案