2017-08-31 52 views
0

當視圖加載我從一個API調用填充數據,並將其顯示在單元格:集合視圖細胞刷新沒有發生

override func viewDidLoad() { 
    super.viewDidLoad() 
    parseData(urll: url) 
} 

當我使用我填充了一組新的搜索欄搜索使用此代碼項目:

func searchBarSearchButtonClicked(_ searchBar: UISearchBar) { 

    let keywords = searchBar.text 
    let finalKey = keywords?.replacingOccurrences(of: " ", with: "+") 
    let finalurl = "https://api.giphy.com/v1/gifs/search?api_key=0d4f9877de6d4927943adf5a0d20e8a0&q=\(finalKey!)&limit=25&offset=0&rating=G&lang=en" 
    parseData(urll: finalurl) 
      self.view.endEditing(true) 
} 

但是,當它加載新項目的老項目不被刪除,我已經使用重新加載數據嘗試,但沒有奏效。

而且我已經在parseData()函數中重載數據函數。

parsedata()代碼:

func parseData(urll : String) { 
    var request = URLRequest(url: URL(string: urll)!) 
    request.httpMethod = "GET" 
    let config = URLSessionConfiguration.default 
    let session = URLSession(configuration: config, delegate: nil, delegateQueue: .main) 
    let task = session.dataTask(with: request) { (data, response, error) in 
     if error != nil { 
      print(error!) 
     } 
     else { 
      do { 
       let fetchedData = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as! NSDictionary 
       if let gifArray = fetchedData.value(forKey: "data") as? NSArray { 
        if let gifarrayImg = gifArray.value(forKey: "images") as? NSArray { 
         if let gifarrayImgFixedWdth = gifarrayImg.value(forKey: "fixed_width") as? NSArray { 
          for gif in gifarrayImgFixedWdth { 
           if let gifDict = gif as? NSDictionary { 
            if let imgURL = gifDict.value(forKey: "url") { 
             print(imgURL) 
             self.imgUrlArray.append(imgURL as! String) 
            } 
            if let imgHeight = gifDict.value(forKey: "height") { 
             self.height.append(imgHeight as! String) 
             print(imgHeight) 
            } 
            DispatchQueue.main.async { 
             self.collectionView.reloadData() 
            } 
           } 
          } 
         } 
        } 
       } 
      } 
      catch { 
       print("Error2") 
      } 
     } 
    } 
    task.resume() 
} 

CollectviewdataSource

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
    return imgUrlArray.count 
} 
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as? CollectionViewCell 
    cell?.layer.shouldRasterize = true 
    cell?.layer.rasterizationScale = UIScreen.main.scale 

    let resource = ImageResource(downloadURL: URL(string : imgUrlArray[indexPath.row])!, cacheKey: imgUrlArray[indexPath.row]) 
    cell?.imgview.kf.setImage(with: resource) 


    return cell! 
} 
+0

請添加parseData()函數的代碼。 –

+0

在parseData()中調用'self.collectionView.reloadData()嗎? – Dopapp

+0

是主隊列中的@Dopapp –

回答

0

,因爲要附加從搜索到現有imageUrlArray特性,進行圖像的URL的老項目不被刪除。因此,您可以在進行搜索請求之前刪除該數組中的所有項目。只需在parsedata()函數頂部加上這一行:

self.imageUrlArray.removeAll(keepingCapacity: false) 
+0

我實現了它,但現在我得到一個錯誤 致命錯誤:索引超出範圍 –

+0

我得到了解決方案,我需要在搜索功能再次調用reloadData函數。 –