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 に答える