How to fill UITableView with a data from Dictionary. Swift

后端 未结 3 649
臣服心动
臣服心动 2020-12-21 11:54

Please help me with filling table view cells with data from a dictionary. For instance, I have cell like so:

and for filling it with data I\'ve started with

相关标签:
3条回答
  • 2020-12-21 12:11

    By using this:

    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
    }
    
    0 讨论(0)
  • 2020-12-21 12:15

    I used a struct Rate to Reproduce your current Output

    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
        }
    }
    

    Currencies Array

    /// 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)]
    

    Get All Keys in Separate Array so we can Dequeue cell Easily

    var keysArray = Array(currencies.keys)
    

    TableView Function

    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
    }
    

    Playground Output

    Hope this helps

    0 讨论(0)
  • 2020-12-21 12:19

    Using only one array is better for tableView data instead of using dictionary or more arrays. To do this, you can create a struct to wrap all data a cell needs.

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

    You can turn dictionary to Currency array or assign your data to Currency at first.

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

    Then use this array to cell.

    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
    }
    
    0 讨论(0)
提交回复
热议问题