2017-08-31 9 views
0

が起こっていないリフレッシュこのコードを使用してアイテム:コレクションビューの細胞が

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()コード:既存のimageUrlArrayプロパティに、検索からの画像のURLを追加しているので

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

古いアイテムが削除されていません。したがって、検索要求を行う前に、その配列内のすべての項目を削除することができます。 parsedata()関数の先頭に次の行を追加してください:

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

私はそれを実装しましたが、今は という致命的なエラーのエラーが発生しています。インデックスが範囲外です –

+0

検索機能でreloadData関数を再度呼び出す必要があるソリューションがあります。 –