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
的值。请给我解决方案。
答案 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)
结果: Tableview将在0个单元格中正常加载,并在数据存在时刷新所有数据。
答案 2 :(得分:1)
在完成程序段中重新加载您的tableview,从打印您的响应或您的for循环结束。像,
tableView.reloadData()
并确保tempData
和tempData[indexPath.row]["Name"] as? String
不是零。通过放置print
语句或放置breakpoints
来检查它。