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

标签 ios swift uitableview closures

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/

相关文章:

swift - Xcode 7 如何获取文本字段输入?

ios - UICollectionViewCell 在 UICollectionViewReusableView 宽度之后启动

ios - 如何在不关闭应用程序的情况下打开应用程序商店链接

ios - 带有用户照片的相机叠加层未保存为已编辑

ios - 如何在 uicollectionview header 中添加 uicollectionview

swift - 将自定义 key 添加到 info.plist,当我从 Swift 中的 Playground 访问时,它是零

ios - 在获取 PFQuery 对象时更新 View

ios - 如何在 Swift 4 中解析 A​​lamofire 中的数据

ios - 在表格 View 中定位自定义单元格未完全显示单元格

iphone - 在 ViewController 中启用编辑模式