1

CollectionView を手伝ってくれる人はいますか? 一部の API から JSON をパースし、文字列値 (タイトルと画像のアドレス) を取得し、そのデータをメソッド cellForItem() でカスタム collectionViewCell に割り当てます。しかし、コレクションビューを高速スクロールするよりも、一部のセルに間違った画像や重複した画像が含まれている可能性があります。また、カスタム セル クラスでメソッド prepareForReuse をオーバーライドし、 cell.image = UIImage() を設定しましたが、機能しませんでした。何が間違っているのか理解してください)私の下手な英語で申し訳ありません...

コレクションViewController

'''

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Result",
                                                      for: indexPath) as! ResultCell
        let film = results[indexPath.item]
        cell.name.text = film.title
        cell.imageView.layer.borderColor = UIColor(white: 0, alpha: 0.3).cgColor
        cell.imageView.layer.borderWidth = 2
        cell.imageView.layer.cornerRadius = 10
        cell.layer.cornerRadius = 10
        // setUp image for cell
        cell.imageView.image = nil
        contentManager.getImage(film.poster.image, cell.imageView)
        return cell
    }

'''

メソッド取得画像 '''

func getImage(_ url_str:String, _ imageView:UIImageView) {
        let url:URL = URL(string: url_str)!
        let session = URLSession.shared
        let task = session.dataTask(with: url, completionHandler: {(data, response, error) in
            if data != nil {
                let image = UIImage(data: data!)
                if(image != nil) {
                    DispatchQueue.main.async(execute: {
                        imageView.image = image
                        imageView.alpha = 0
                        UIView.animate(withDuration: 1, animations: {
                            imageView.alpha = 1.0
                        })
                    })
                }
            }
        })
        task.resume()
    }

'''

セル カスタム クラス '''

class ResultCell: UICollectionViewCell {
    @IBOutlet  var imageView: UIImageView!
    @IBOutlet var name : UILabel!
    
    
    override func prepareForReuse() {
       
        self.imageView.image = UIImage()
        self.name.text = nil
         super.prepareForReuse()
    }
}

'''

4

0 に答える 0