【问题标题】:How to fill UITableView with a data from Dictionary. Swift如何用 Dictionary 中的数据填充 UITableView。迅速
【发布时间】:2018-07-01 21:41:34
【问题描述】:

请帮助我用字典中的数据填充表格视图单元格。例如,我有这样的单元格:

为了用数据填充它,我开始重写cellForRowAt 方法:

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "CurrencyCell", for: indexPath) as! CurrencyCell

    for (key, value) in currencies! {
        print("key is - \(key) and value is - \(value)")
    }

    // ?

    // cell.currencyLabel.text =
    // cell.askLabel.text =
    // cell.bidLabel.text =

    return cell
}

字典的打印输出在这里:

key is - EUR and value is - Rate(ask: Optional("30.8500"), bid: Optional("30.1000"))
key is - USD and value is - Rate(ask: Optional("26.3000"), bid: Optional("26.0500"))
key is - RUB and value is - Rate(ask: Optional("0.4150"), bid: Optional("0.3750"))

如何做到这一点?提前致谢!

【问题讨论】:

  • 将您的数据放入一个数组中,按照您希望它在表格中的显示方式排序。

标签: ios swift uitableview


【解决方案1】:

我使用 struct Rate 来重现您当前的输出

struct Rate {
    var ask : Float?
    var bid : Float?

    static var shared = Rate()

    mutating func initWithDictValues(_ currentRate : Rate) {
        self.ask = currentRate.ask
        self.bid = currentRate.bid
    }
}

货币数组

/// Array Declaration
var currencies = [String:Any]()

/// Add Values
currencies = ["EUR":Rate(ask: 30.8500, bid: 30.8500),"USD":Rate(ask: 26.3000, bid: 26.3000),"RUB":Rate(ask: 0.4150, bid: 0.4150)]

获取单独数组中的所有键,以便我们可以轻松地将单元格出列

var keysArray = Array(currencies.keys)

表格视图功能

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "CurrencyCell", for: indexPath) as! CurrencyCell

    /// Get CurrentKey
    let currentKey = keysArray[indexPath.row]
    let currentIndexKey : Rate = currencies[currentKey] as! Rate

    /// Assign Values
    cell.currencyLabel.text = currentKey
    cell.askLabel.text = currentIndexKey.ask ?? 0
    cell.bidLabel.text = currentIndexKey.bid ?? 0

    return cell
}

游乐场输出

希望对您有所帮助

【讨论】:

    【解决方案2】:

    通过使用这个:

    var valueArray: [Int] = []
    var currencyArray: [String] = []
    override func viewDidLoad() {
        super.viewDidLoad()
    
        for (key, value) in currencies! {
            print("key is - \(key) and value is - \(value)")
            currencyArray.append(key)
            valueArray.append(value)            
        }
    
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "CurrencyCell", for: indexPath) as! CurrencyCell
    
        //retrieve the key and value
        let value = valueArray[indexPath.row]
        let key = currencyArray[indexPath.row]
    
        //next use the value and key for what you need them
    
        return cell
    }
    

    【讨论】:

    • 感谢您发布您的答案。我已经使用:let keys = Array(currencies!.keys); let values = Array(currencies!.values) 完成了这项工作。但无论如何,我会支持你的答案,因为你投入了你的时间和精力:)
    • 拥有两个单独的数组是错误的方法。你想要一个数组。多个数组使排序和过滤变得更加困难。
    • @rmaddy,你能在这里举个例子吗?
    • @HarryMcGovern 查看使用struct 的任何其他答案。
    【解决方案3】:

    对于 tableView 数据,只使用一个数组比使用字典或多个数组更好。 为此,您可以创建一个结构来包装单元格所需的所有数据。

    struct Currency { 
        let currency: String
        let rate: Rate
    }
    

    您可以先将字典转换为 Currency 数组或将您的数据分配给 Currency。

    var currencies = [Currency]()
    for (key, value) in currencies! {
        let currency = Currency(currency: key, rate: value)
        currencies.append(currency)          
    }
    

    然后使用这个数组来单元格。

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "CurrencyCell", for: indexPath) as! CurrencyCell
        let currency = currencies[indexPath.row]
    
        //assign data to cell
    
        return cell
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多