2016-06-07 28 views
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(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的直接子帳戶。在第一個請求的完成塊中,我試圖獲取盛大的子帳戶。 dataRows有部分標題(這是直接的子名稱)的數據。 dataOfGrandChilds是將部分編號保存爲鍵並將相應的grandChilds數組作爲其值的字典。使用部分和tableviewcells在使用SFRestAPI時一起獲取錯誤

enter image description here

在重裝我的tableView我能夠只顯示第1段的孩子而不是第二部分孩子。請幫助找到解決方案。

的錯誤我得到是

2016年6月7日11:10:3​​0.764 ICRM [67964:11218133] ***終止應用程序由於 未捕獲的異常 'NSRangeException',原因: ' - [__ NSCFArray objectAtIndex:]:索引(1)超過界限(1)'

+0

它會崩潰哪行!?您試圖訪問僅包含1個元素的數組的第二個元素 – Paulw11

+0

cell.textLabel?.text = tempData [indexPath.row] [「Name」] as?字符串 @ Paulw11 在這一行中,它正在崩潰。 –

+0

正確,因此您正在訪問基於'dataOfGrandchilds [section]'中的條目的'tempData'數組,但'numberOfRowsInSection'不會基於相同的數據返回值。它返回1或部分的數量。 – Paulw11

回答

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