从YQL请求显示的股票价格不正确(Swift)

时间:2017-01-11 16:47:35

标签: ios swift

我有一个从YQL获取当前股票价格的功能(使用Alamofire):

func stockFetcher(completion: @escaping ([String]?) -> Void) {
Alamofire.request(stockUrl).responseJSON { (responseData) -> Void in
    if((responseData.result.value) != nil) {
        let json = JSON(responseData.result.value!)
        if let appleStockPrice = json["query"]["results"]["quote"][0]["Ask"].string {
            prices.append(appleStockPrice)
        }
        if let googleStockPrice = json["query"]["results"]["quote"][1]["Ask"].string {
            prices.append(googleStockPrice)
        }
        if let twitterStockPrice = json["query"]["results"]["quote"][2]["Ask"].string {
            prices.append(twitterStockPrice)
        }
        if let teslaStockPrice = json["query"]["results"]["quote"][3]["Ask"].string {
            prices.append(teslaStockPrice)
        }
        if let samsungStockPrice = json["query"]["results"]["quote"][4]["Ask"].string {
            prices.append(samsungStockPrice)
        }
        completion(prices)
        print(json)
    }
}
}

这是返回的JSON,除了Twitter的null之外,一切看起来都不错,但这是一个单独的问题:

{
"query" : {
"created" : "2017-01-11T16:34:51Z",
"results" : {
  "quote" : [
    {
      "symbol" : "AAPL",
      "YearLow" : "89.4700",
      "YearHigh" : "119.3600",
      "Ask" : "119.1100"
    },
    {
      "symbol" : "GOOG",
      "YearLow" : "663.06",
      "YearHigh" : "816.68",
      "Ask" : "805.08"
    },
    {
      "symbol" : "TWTR",
      "YearLow" : "13.730",
      "YearHigh" : "25.250",
      "Ask" : null
    },
    {
      "symbol" : "TSLA",
      "YearLow" : "141.0500",
      "YearHigh" : "269.3400",
      "Ask" : "227.8600"
    },
    {
      "symbol" : "SSNLF",
      "YearLow" : "1000.00",
      "YearHigh" : "1600.00",
      "Ask" : null
    }
  ]
},
"count" : 5,
"lang" : "en;q=1.0"
  }
}

在我的tableview中,我显示了这样的价格(在cellForRow中):

    stockFetcher(completion: {
        (prices) -> Void in

        if (prices?.count)! > indexPath.row + 1 {
            cell.detailTextLabel?.text = "Current Stock Price: \(prices![indexPath.row])"
        } else {
            cell.detailTextLabel?.text = "No data found"
        }
    })

前3个显示正确的价格,但特斯拉显示苹果当前的价格(在这种情况下为119)和三星显示谷歌的价格(在这种情况下为804) - 似乎前3个价格出现然后开始重复。

任何人都可以看到问题所在吗?

编辑:cellForRow

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! CompanyCell

    // Labels
    cell.textLabel?.text = companyNames[indexPath.row]

    // Detail labels
    stockFetcher(completion: {
        (prices) -> Void in

        if (prices?.count)! > indexPath.row + 1 {
            cell.detailTextLabel?.text = "Current Stock Price: \(prices![indexPath.row])"
        } else {
            cell.detailTextLabel?.text = "No data found"
        }
    })

    return cell
}

1 个答案:

答案 0 :(得分:0)

您的stockFetcher异步执行某项操作(带有完成块的网络请求),而完成块保留对可能重复使用的单元格的引用。

当Alamofire返回数据时,该单元格可能正在其他地方使用。你不应该坚持使用单元格引用并执行异步操作。相反,您应该异步地获取表视图数据源方法之外的数据,然后在触发cellForRowAt时,只需从存储的数据中读取数据。