cellForRowAtIndexPath在源获取数据之前执行

时间:2016-06-06 11:00:24

标签: ios swift uitableview

override func viewDidLoad() {
    super.viewDidLoad()
    print("loaded hie \(accountId)")
    let request = SFRestAPI.sharedInstance().requestForQuery("SELECT Name,Id FROM Account where parentid='\(self.accountId)'");
    //SFRestAPI.sharedInstance().send(request, delegate: self);
    SFRestAPI.sharedInstance().sendRESTRequest(request, failBlock: {error in print(error)}, completeBlock: { responce in print(responce)
        self.dataRows = responce["records"] as! [NSDictionary]
        var counter = 0;
        for i in self.dataRows
        {
            let requestForGrandChilds = SFRestAPI.sharedInstance().requestForQuery("select Name,Id from Account where parentid='\(i["Id"]!)'")
            SFRestAPI.sharedInstance().sendRESTRequest(requestForGrandChilds,
            failBlock:
            {
                error in print(error)
                print("error block")
            },
            completeBlock:
            {
                responceChild in
                self.grandChilds = responceChild["records"] as! [NSDictionary]
                print(self.grandChilds)
                self.dataOfGrandChilds["\(counter)"] = self.grandChilds
                print(self.dataOfGrandChilds)
                counter += 1

                //Reloading My tableView
                self.tableView.reloadData()
            })
        }
    })
}

这是我的viewDidLoad()。 这里dataOfGrandChilds是一个字典,它是我的表视图单元格的源。

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("toViewChilds", forIndexPath: indexPath)
    print("loading content\(self.dataOfGrandChilds)")
    if let tempData = dataOfGrandChilds[indexPath.section]
    {
        cell.textLabel?.text = tempData[indexPath.row]["Name"] as? String
    }
    return cell
}

这是我的cellForRowAtIndexPath

问题出在dataForGrandChilds保留viewDidLoad()中的内容之前,cellForRowAtIndexPath()正在执行。这就是为什么我无法填充dataForGrandChilds的值。请给我解决方案。

3 个答案:

答案 0 :(得分:2)

您只需要重新加载表格视图

yourTableView.reloadData()

在您的网络请求完成数据后,请输入此代码。

您可以在completeBlock:

的末尾添加它
override func viewDidLoad() {
    super.viewDidLoad()
    print("loaded hie \(accountId)")
    let request = SFRestAPI.sharedInstance().requestForQuery("SELECT Name,Id FROM Account where parentid='\(self.accountId)'");
    //SFRestAPI.sharedInstance().send(request, delegate: self);
    SFRestAPI.sharedInstance().sendRESTRequest(request, failBlock: {error in print(error)}, completeBlock: { responce in print(responce)
        self.dataRows = responce["records"] as! [NSDictionary]
        var counter = 0;
        for i in self.dataRows
        {
            let requestForGrandChilds = SFRestAPI.sharedInstance().requestForQuery("select Name,Id from Account where parentid='\(i["Id"]!)'")
            SFRestAPI.sharedInstance().sendRESTRequest(requestForGrandChilds,
            failBlock:
            {
                error in print(error)
                print("error block")
            },
            completeBlock:
            {
                responceChild in
                self.grandChilds = responceChild["records"] as! [NSDictionary]
                print(self.grandChilds)
                self.dataOfGrandChilds["\(counter)"] = self.grandChilds
                print(self.dataOfGrandChilds)
                counter += 1

                //RELOAD TABLE VIEW HERE
                yourTableView.reloadData()
            })
        }
    })
}

答案 1 :(得分:1)

  1. 将一个空数组分配给self.dataOfGrandChilds
  2. 在completeBlock的末尾添加一个reloadData,以便
  3. 结果: Tableview将在0个单元格中正常加载,并在数据存在时刷新所有数据。

答案 2 :(得分:1)

在完成程序段中重新加载您的tableview,从打印您的响应或您的for循环结束。像,

  tableView.reloadData()

并确保tempDatatempData[indexPath.row]["Name"] as? String不是零。通过放置print语句或放置breakpoints来检查它。