2017-03-06 30 views
0

我得到這個錯誤:斯威夫特泰伯維刷新錯誤

image 1

這是我的代碼: image 2

我在該項目的tableView部分使用刷新。刷新過程中可能導致此錯誤的原因是什麼?

But in which phase it falls to the fault I could not solve that part

var kategoriId = "" 
    var refresher = UIRefreshControl() 

    var arrayKonularData = [konularData]() 
    let singleton = konularClass.sharedGlobal 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     refresher.attributedTitle = NSAttributedString(string: "Yükleniyor") 
     refresher.addTarget(self, action: #selector(KonuDetayViewController.refresh), for: UIControlEvents.valueChanged) 
     self.tableview.addSubview(refresher) 


     KonulariGetir(sirala: "order by tarih desc") 
     navigationController?.delegate = self 
     tableview.layer.cornerRadius = 10 
    } 
    func refresh() 
    { 
     DispatchQueue.main.async { 
      if self.segmentControl.selectedSegmentIndex == 0 
      { 
       self.arrayKonularData.removeAll() 
       self.KonulariGetir(sirala: "order by tarih desc") 
      } 
      if self.segmentControl.selectedSegmentIndex == 1 
      { 
       self.arrayKonularData.removeAll() 
       self.KonulariGetir(sirala: "order by indirimpuani desc") 
      } 

     } 
       DispatchQueue.main.async { 
      self.refresher.endRefreshing() 
     } 

    } 

I am taking data from web service in this section

func KonulariGetir(sirala:String) 
    { 
     var request = URLRequest(url: URL(string:"http://212.xxx.xxx.xxx:7001/IndirimiKovala/KonuGetir")!) 
     request.httpMethod = "POST" 
     let postString = "filtre="+sirala 
     request.httpBody = postString.data(using: .utf8) 
     let task = URLSession.shared.dataTask(with: request) { (data, response, error) in 

      if error != nil 
      { 
       print("error") 
      } 

      if let urlContent = data 
      { 

       do 
       { 
        let jsonResult = try JSONSerialization.jsonObject(with: urlContent, options: JSONSerialization.ReadingOptions.mutableContainers) as AnyObject 

        if let gelenDizi = jsonResult as? NSArray 
        { 
         for i in 0..<gelenDizi.count 
         { 

          if let baslik = (gelenDizi[i] as? NSDictionary)?["baslik"] as? String 
          { 
           self.singleton.baslik = baslik 
          } 

          if let indirimPuani = (gelenDizi[i] as? NSDictionary)?["indirimpuani"] as? Int 
          { 
           self.singleton.indirimPuani = String(indirimPuani) 
          } 
          if let konuId = (gelenDizi[i] as? NSDictionary)?["id"] as? Int 
          { 
           self.singleton.konuId = String(konuId) 
          } 
          if let haberVeren = (gelenDizi[i] as? NSDictionary)?["uye"] as? String 
          { 
           self.singleton.haberVerenUye = haberVeren 
          } 
          if let gelenTarih = (gelenDizi[i] as? NSDictionary)?["tarih"] as? String 
          { 
           self.singleton.tarih = gelenTarih 
          } 
          if let gelenAktif = (gelenDizi[i] as? NSDictionary)?["aktif"] as? Int 
          { 
           self.singleton.aktif = gelenAktif 
          } 


          self.arrayKonularData.append(konularData.init(baslik: self.singleton.baslik, indirimPuani: self.singleton.indirimPuani, konuId: self.singleton.konuId,haberVeren:self.singleton.haberVerenUye , tarih:self.singleton.tarih,aktif:self.singleton.aktif)) 


         } 

        } 
        DispatchQueue.main.async { 
         self.tableview.reloadData() 
        } 


       } 
       catch 
       { 
        print("server hatası") 
       } 
      } 
     } 
     task.resume() 
    } 
+1

請添加代碼的問題,而不是代碼的圖片。 –

+0

由於控制檯中的錯誤顯示「索引超出範圍」。 。 。 另外:你不要添加一個'UIRefreshControl'作爲子視圖。在你使用的'UITableView'上有'refreshControl'屬性。 http://cleanswifter.com/uirefreshcontrol-in-ios10/。 (不知道這是否是問題的一部分,但仍值得修復) – shim

+1

控制檯說這是*超出範圍*錯誤 – vadian

回答

0

我想這個問題來自哪裏,你嘗試填充tableview中的代碼的一部分。因此,可能的解決方案可以在的tableView(_的tableView:UITableView的,cellForRowAt indexPath:IndexPath)委託梅索德檢查,如果arrayKonularData數組不爲空這樣

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 
    { 
     let cell = tableView.dequeueReusableCell(withIdentifier: orderCell, for: indexPath) 


     if !arrayKonularData.isEmpty { 
      .....// Your code 
     } 

     return cell 
    } 

另一種解決方案(我的東西將是正確的解決方案的情況)加

func KonulariGetir(sirala:String) 

完成功能和重裝的tableview在完井方法

+0

如果'arrayKonularData'爲空,則numberOfRows將返回0,並且不會調用'cellForRow' 。 – vadian

+0

是的,你是對的。所以這個問題不是來自那個部分。從提供有問題的代碼部分,很難找到可能導致此錯誤的原因 –

+0

當互聯網速度慢時,會出現此錯誤 –