2017-08-01 54 views
2

我通過JSON獲取數據,當我滾動屏幕時,我有一個請求到服務器,有時數據可以結束,然後應用程序崩潰insertRows我該如何解決它?insertView在表查看

這裏我的代碼:

override func scrollViewDidScroll(_ scrollView: UIScrollView) { 
    if (!isMoreDataLoading) { 

     let down = "https://--.com/local/apps/apple/events.php/?nPageSize=\(nPageSize)&iNumPage=\(iNumPage)" 
     let scrollViewContentHeight = tableView.contentSize.height 
     let scrollOffsetThreshold = scrollViewContentHeight - tableView.bounds.size.height 
     if(scrollView.contentOffset.y > scrollOffsetThreshold && tableView.isDragging) { 
      isMoreDataLoading = true 
      requestD(url: down) 
     } 
    } 
} 
func requestD(url:String){ 

    var urlRequest = URLRequest(url: URL(string: url)!) 
    urlRequest.timeoutInterval = 300 
    let task = URLSession.shared.dataTask(with: urlRequest) { (data,response,error) in 

     if error != nil{ 
      print(error ?? 0) 
      return 
     } 
     do{ 
      let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as! [String : AnyObject] 
      if let newsJson = json["EVENTS"] as? [[String : AnyObject]] { 

       for newJson in newsJson{ 
        let info = Modal() 

        info.id = newJson["ID"] as? String 
        info.name = newJson["NAME"] as? String 

        if newJson["PICTURE"] as? String != nil { 
         info.ImageViewURL = newJson["PICTURE"] as! String 
        } 
        if newJson["PICTURE_DETAIL"] as? String != nil { 
         info.ImageViewURLDetail = newJson["PICTURE_DETAIL"] as! String 
        } 
        info.stmp = newJson["STMP"] as? Int 
        info.text = newJson["DETAIL_TEXT"] as? String 


        info.name = info.name?.replacingOccurrences(of: "&[^;]+;", with: "", options: String.CompareOptions.regularExpression, range: nil) 
        info.text = info.text?.replacingOccurrences(of: "&[^;]+;", with: "", options: String.CompareOptions.regularExpression, range: nil) 


        if self.isRefresh == true { 
         self.modals.insert(info, at: 0) 
        } else { 
         self.modals.append(info) 
        } 



       } 
      } 

     } catch let error { 
      print(error) 
     } 

     DispatchQueue.main.sync { 

      self.iNumPage += 1 
      self.isMoreDataLoading = false 


      self.Preload.preloadEnd(tableView: self.tableView) 

      // self.tableView.reloadData() 


      self.tableView.beginUpdates() 
      self.tableView.insertRows(at: [IndexPath(row: self.modals.count - 1, section: 0)], with: .automatic) 
      self.tableView.endUpdates() 

     } 
    } 

    task.resume() 
} 

原因:'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (5) must be equal to the number of rows contained in that section before the update (0), plus or minus the number of rows inserted or deleted from that section (1 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'

+0

什麼是崩潰消息? – Paulw11

+0

@ Paulw11有問題的原因 – Vasya2014

回答

0

如果要刪除/ TableView中插入一個元素,你需要做到以下幾點:

  1. 添加數據在tableView的後備數據源中。
  2. 重新載入tableView。

一些代碼來說明點(此代碼被放置在didSelectRowAtIndexPath方法):

anArray.insert("This String", atIndex: indexPath.row) 
tableView.reloadData() 
2

當設置新的表格行您沒有在陣列中添加新數據。 在表中插入新行之前添加此代碼self.modals.insert("<#DATA#>, atIndex: yourIndex)。並嘗試刪除DispatchQueue

+0

你不明白我的問題。我需要鎖定,如果沒有數據時,用戶滾動下來 – Vasya2014

+1

因此,更新表中的塊在哪裏你添加新行 –

+0

由於數據任務需要時間和插入表正在調用兩個代碼不同步,您的表插入代碼不在等待數據來 –