gpt4 book ai didi

ios - 在使用 SFRestAPI 时一起使用部分和 tableviewcells 时出错

转载 作者:行者123 更新时间:2023-11-28 06:42:03 25 4
gpt4 key购买 nike

var accountId = String()
var dataRows = [NSDictionary]()
var grandChilds = [NSDictionary]()
var dataOfGrandChilds = NSMutableDictionary()
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]
self.dataOfGrandChilds["\(counter)"] = self.grandChilds
print(self.dataOfGrandChilds)
counter += 1
print("control still in inner competion block")
dispatch_async(dispatch_get_main_queue(),
{ () -> Void in
print("Control came to main queue")
self.tableView.reloadData()
})
})
}

})

}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
if(dataOfGrandChilds.count > 0 ){
return dataOfGrandChilds.count
}
return 1
}

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

在第一个请求中,我尝试获取一个 ID 的直接子帐户。在第一个请求的完成 block 中,我正在尝试获取 grand child accounts 。 dataRows 有部分标题的数据(这是直接的子名称)。 dataOfGrandChilds 是一个字典,它将节号作为键,相应的 grandChilds 数组作为它的值。

enter image description here

在重新加载我的 tableView 时,我只能显示第一个部分的 child ,而不是第二个部分的 child 。请帮助寻找解决方案。

我得到的错误是

2016-06-07 11:10:30.764 iCRM[67964:11218133] *** Terminating app due to uncaught exception 'NSRangeException', reason: '-[__NSCFArray objectAtIndex:]: index (1) beyond bounds (1)'

最佳答案

var accountId = String()
var dataRows = [NSDictionary]()
var grandChilds = [NSDictionary]()
var dataOfGrandChilds = NSMutableDictionary()
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]
self.dataOfGrandChilds["\(counter)"] = self.grandChilds
print(self.dataOfGrandChilds)
counter += 1
print("control still in inner competion block")
dispatch_async(dispatch_get_main_queue(),
{ () -> Void in
print("Control came to main queue")
self.tableView.reloadData()
})
})
}

})

}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
if let countOfRows = dataOfGrandChilds[section]?.count // to avoid un-wrapping nil value
{
return countOfRows
}
return 1
}





override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return dataRows[section]["Name"] as? String
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("toViewChilds", forIndexPath: indexPath)
print(indexPath.section)
print(dataOfGrandChilds["\(indexPath.section)"])
if let tempData = dataOfGrandChilds["\(indexPath.section)"]
{
if(tempData.count != 0 )//to avoid array index out of bound exceptions
{
cell.textLabel?.text = tempData[indexPath.row]["Name"] as? String
}
}
return cell
}

所做的更改是:

  1. numberOfRowsInSection(): 返回正确的行数
  2. cellForRowAtIndexPath(): tempData.count != 0 因为我们需要避免 NSRangeException

关于ios - 在使用 SFRestAPI 时一起使用部分和 tableviewcells 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37672336/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com