Ios cellForRowAtIndexPath在源获取数据之前执行
这是我的Ios cellForRowAtIndexPath在源获取数据之前执行,ios,swift,uitableview,Ios,Swift,Uitableview,这是我的viewDidLoad()。 这里的dataOfGrandChilds是一个字典,它是我的表视图单元格的源代码 override func viewDidLoad() { super.viewDidLoad() print("loaded hie \(accountId)") let request = SFRestAPI.sharedInstance().requestForQuery("SELECT Name,Id FROM Account where par
viewDidLoad()
。
这里的dataOfGrandChilds
是一个字典,它是我的表视图单元格的源代码
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()
})
}
})
}
这是我的cellforrowatinexpath
问题是在
dataForGrandChilds
保存viewDidLoad()
中的内容之前,CellForRowatingIndexPath()
正在执行。这就是为什么我无法填充dataForGrandChilds
的值。请给我解决方案。您只需重新加载表视图即可
yourTableView.reloadData()
将此代码放在web请求完成数据之后
例如,您可以在completeBlock:
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
}
您只需重新加载表视图
yourTableView.reloadData()
将此代码放在web请求完成数据之后
例如,您可以在completeBlock:
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
}
Tableview将加载0个单元格,并在数据存在时刷新所有数据。从打印响应或for循环结束的位置在完成块中重新加载Tableview。大概
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()
})
}
})
}
并确保
tempData
和tempData[indexPath.row][“Name”]为?字符串
不是零。将print
语句或breakpoints
从打印响应或for循环结束的位置将tableview重新加载到完成块中,以检查它。大概
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()
})
}
})
}
并确保
tempData
和tempData[indexPath.row][“Name”]为?字符串
不是零。通过在主线程dispatch\u async(dispatch\u get\u main\u queue()){self.tableView.reloadData()}
语句中放置断点来检查它,而不是在web请求结束时,在viewDidLoad
的末尾放置断点请参阅更新的代码片段@ramcharanredynow我尝试了。没有结果@EvgenyKarkan dispatch_async(dispatch_get_main_queue()){self.tableView.reloadData()}我也尝试过。没有结果。检查您的数据源数组-它是否存在,是否为空?我可以打印我的数据源数组@EvgenyKarkan的内容,您可以在上面的code@RamcharanReddy在主线程中重新加载数据dispatch\u async(dispatch\u get\u main\u queue()){self.tableView.reloadData()}
No,不是在viewDidLoad
的末尾,也不是在web请求的末尾;)请参阅更新的代码片段@ramcharanredynow我尝试了。没有结果@EvgenyKarkan dispatch_async(dispatch_get_main_queue()){self.tableView.reloadData()}我也尝试过。没有结果。请检查数据源数组-它是否存在,它是否为空?我可以打印我的数据源数组@EvgenyKarkan的内容,你可以在上面的代码中使用c做同样的事情,但重新加载实际上不起作用@Helge BeckerDid同样的事情,但重新加载实际上不起作用@Helge BeckerI我已经尝试将“tableView.reloadData()”放在我循环的最后一行,如上所述。但它不起作用@你检查过你的数据了吗tempData
,dataOfGrandChilds
,self.dataRows
等????如果让tempData=dataOfGrandChilds[indepath.section]
获取数据,那么您确定它是indepath.section
而不是indepath.row
?Yea Yea@Lion。。。我对那件事很有把握。我使用节号作为特定数组的键。dataOfGrandChilds是以节号为键并对应为子级的字典。我也试过放置断点和打印语句。我试过像这样的调度异步(调度获取主队列,{self.tableView.reloadData}),但我还是没有得到它。@Liondispatch\u异步(调度获取主队列()){()->Void in self.tableView.reloadData}我认为应该是这样的!我已尝试将“tableView.reloadData()”放在循环的最后一行,如上所述。但它不起作用@你检查过你的数据了吗tempData
,dataOfGrandChilds
,self.dataRows
等????如果让tempData=dataOfGrandChilds[indepath.section]
获取数据,那么您确定它是indepath.section
而不是indepath.row
?Yea Yea@Lion。。。我对那件事很有把握。我使用节号作为特定数组的键。dataOfGrandChilds是以节号为键并对应为子级的字典。我也试过放置断点和打印语句。我试过像这样的调度异步(调度获取主队列,{self.tableView.reloadData}),但我还是没有得到它。@Liondispatch\u异步(调度获取主队列()){()->Void in self.tableView.reloadData}我认为应该是这样的!