如何正确加载依赖的tableViewCell API数据?

时间:2015-04-09 13:13:19

标签: ios json uitableview swift

我一直在研究一个UITableView,它现在为每个单元格进行2次API调用。它一直很好。然而今天我遇到了一个大问题。这是第一次,屏幕上的细胞数量超过装载细胞的数量。

所以当我向下滚动我的桌子时,屏幕冻结了几秒钟,因为它没有加载最后一个单元格。

我一直试图正确加载这些数据的问题。这是我对每个单元格的第二个API调用取决于第一个。

以下是目前的设置方式:

我的tableView

// #tableView
    tableView.setTranslatesAutoresizingMaskIntoConstraints(false)
    tableView.backgroundColor = UIColor.formulaWhiteColor()
    tableView.frame         =   CGRectMake(0, 0, 0, 0)
    tableView.delegate      =   self
    tableView.dataSource    =   self
    self.tableView.tableFooterView = UIView(frame: CGRectZero)
    self.tableView.separatorColor = UIColor.clearColor()
    tableView.registerClass(mySuggestionsCell.self, forCellReuseIdentifier: "com.Formula.mySuggestionsCell")
    tableView.addSubview(refreshControl)
    self.view.addSubview(tableView)

当我的视图显示时,我运行此功能:

func loadSuggestions() {
    DNService.suggestionsForSection() { (JSON) -> () in
        self.suggestions = JSON["actionable"]
        self.tableView.reloadData()
        self.refreshControl.endRefreshing()
    }
}

(我的DNSService是使用Alomafire的结构)

struct DNService {

static func suggestionsForSection(response: (JSON) -> ()) {
    let urlString = suggestionsURL
    Alamofire.request(.GET, urlString).responseJSON { (_, _, data, _) -> Void in
        let suggestions = JSON(data ?? [])
        response(suggestions)
    }
}
}

从那里开始设置细胞:

行数:

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

实际配置:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("com.Formula.mySuggestionsCell", forIndexPath: indexPath) as! mySuggestionsCell
    let suggestion = suggestions[indexPath.row]
    cell.configureWithSuggestion(suggestion)
    cell.selectionStyle = UITableViewCellSelectionStyle.None
    return cell
}

其余部分目前正在我的UITableViewCell中进行,我很确定我在这方面做错了。

func configureWithSuggestion(suggestion: JSON) {
    tickerString = suggestion["ticker"].string!
    tickerLabel.text = tickerString

    // I'm setting up other things like above, but cut them out as they aren't relevant.

    var quoteAPI = NSURL(string: "http://dev.markitondemand.com/Api/v2/Quote/json?symbol=\(tickerString)")
    // This is the API that depends on data from the first API.
    // This is being called for every cell that's being configured...

    var request = NSURLRequest(URL: quoteAPI!)
    var data = NSURLConnection.sendSynchronousRequest(request, returningResponse: nil, error: nil)
    if data != nil {
        var quote = JSON(data: data!)
        lastPrice = quote["LastPrice"]
        // Again calling more data than just this.
    }
    lastPriceLabel.text = lastPrice
    // I'm also setting up and animating some charts here.
}

当其中一个调用取决于第一个调用时,如何最好地检索和设置此API数据?

1 个答案:

答案 0 :(得分:0)

我最终为数据创建了一个coreData实体。

然后当视图加载时,我调用第一个api,循环遍历它,将它保存到coreData,在循环中我也可以使用变量来调用第二个api。

然后当我从coreData加载我的单元格而不是在向下滚动时进行api调用时,加载时间没有问题。