0

我对 iOS 编程相当陌生,在使用完成语句中的变量时遇到了麻烦。我已经包含了下面的代码,我不确定为什么当我将完成变量存储在 dataType 数组中时,它似乎只返回空白字符串。

注意:完成数据在 loadSampleStockData 函数中调用,稍后假设返回到 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 中的单元格

感谢您提供的任何帮助!

import UIKit

class dashboardViewController: DefaultViewController, UITableViewDataSource, UITableViewDelegate {

    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var balanceLabel: UILabel!

    var stocks = [stockData]()
    let stock = stockinfo()

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.delegate = self
        tableView.dataSource = self
        tableView.reloadData()
        loadSampleStockData()
        user.newUser() // Move to login function when login and registration is implemented

        //Sets the Balance Label on Dashboard
        balanceLabel.text = "$" + String(format: "%.2f", user.getBalance())
    }

    func loadSampleStockData () {

        var stock1: stockData = stockData(name: "", askPrice: "", percentageChange: "", stockTicker: "")
        var stock2: stockData = stockData(name: "", askPrice: "", percentageChange: "", stockTicker: "")
        var stock3: stockData = stockData(name: "", askPrice: "", percentageChange: "", stockTicker: "")

        stock.getInfo("FB") {(name, price, change) in dispatch_async(dispatch_get_main_queue(),{
            stock1 = stockData(name: name, askPrice: price, percentageChange: change, stockTicker: "FB")
            stocks.append(stock1)
        })
        }

        stock.getInfo("MSFT") {(name, price, change) in dispatch_async(dispatch_get_main_queue(),{
            stock2 = stockData(name: name, askPrice: price, percentageChange: change, stockTicker: "MSFT")
            stocks.append(stock2)
        })
        }

        stock.getInfo("APPL") {(name, price, change) in dispatch_async(dispatch_get_main_queue(),{
            stock3 = stockData(name: name, askPrice: price, percentageChange: change, stockTicker: "APPL")
            stocks.append(stock3)
        })
        }
        print(stocks.count)
    }

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

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

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cellIdentifier = "stockViewCell"

        let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! stockTableViewCell

        let stock = stocks[indexPath.row]

        cell.stockName.text = stock.name
        cell.stockPercentage.text = stock.percentageChange
        cell.stockDollarChange.text = stock.askPrice
        cell.stockTicker.text = stock.stockTicker

        return cell
    }
}
4

1 回答 1

1

因为stocks += [stock1, stock2, stock3]发生在您的异步调用设置stock1...3为任何内容之前。

的创建stocks应该在完成处理程序中完成。

于 2016-07-05T17:02:42.950 回答