【问题标题】:CollectionView displays last data onlyCollectionView 只显示最后的数据
【发布时间】:2020-02-04 12:36:54
【问题描述】:

我正在使用带有 alamofire 的 youtube api 来获取视频数据(图片 url、标题、描述等)。每当我获取数据并将其附加到我的数组时,单元格都会显示相同的图像。但是当我在“getfeed”方法中打印标题和网址时,显示正确。

查看 cellForItemAtIndexPath。 我认为问题就在这里。 每当我在 cellForItemAt 函数数据重复

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "photoCell", for: indexPath) as! PhotoCell

    //image
    let thumbnailUrl = URL(string: videos[indexPath.item].videoThumnailUrl)

    cell.imageView.kf.setImage(with: thumbnailUrl)

    //prints same title & img url
    print("title cell ",videos[indexPath.item].videoTitle)
    print("thmbUrl cell",videos[indexPath.item].videoThumnailUrl)

    return cell
}

getfeedVideos 函数 但是当我在获取提要功能中打印标题和网址时,它会正确打印。 数据附加在 tempVideos 数组中

func getFeedVideos()  {

    Alamofire.request(videoURL,
                      method: .get,
                      parameters:["part":"snippet",
                                  "regionCode":"US",
                                  "chart":"mostPopular",
                                  "maxResults":3,
                                  "key":API_KEY]).responseJSON {
                                    response in
                                    if response.result.isSuccess {

                                        print("Success! Got the video data")
                                        let videoJSON : JSON = JSON(response.result.value!)
                                        //print(videoJSON)

                                        //creating video object off of JSON response
                                        let videoObj = Video()
                                        var tempVideos = [Video] ()

                                        //loops through all items in videoJson and assigns each value
                                        for (_, value):(String, JSON) in videoJSON["items"] {

                                            videoObj.videoId = value["id"].stringValue
                                            videoObj.videoTitle = value["snippet"]["title"].stringValue
                                            videoObj.videoDescription = value["snippet"]["description"].stringValue
                                            videoObj.videoThumnailUrl = value["snippet"]["thumbnails"]["standard"]["url"].stringValue

                                            //prints different title & img url
                                            print("title ",videoObj.videoTitle)
                                            print("thmbUrl" ,videoObj.videoThumnailUrl)
                                            tempVideos.append(videoObj)
                                        }
                                        self.videos = tempVideos
                                        self.collectionView.reloadData()


                                    } else {
                                        print("Error \(String(describing: response.result.error))")

                                    }

    }

}

视频类对象

import UIKit
class Video: NSObject {

var videoId: String = ""
var videoTitle: String = ""
var videoDescription: String = ""
var videoThumnailUrl: String = ""


}

当我运行最终代码时,所有单元格都显示相同的图像。

【问题讨论】:

    标签: arrays swift uicollectionview youtube-api


    【解决方案1】:

    您的CollectionView 仅显示最后一个数据,因为您没有为每个视频数据创建new instance。您只需创建one instance 并将值分配给same instance 的properties。您必须为每个视频数据创建new instance of Video。

    let videoObj = Video()
    

    你应该在你的for loop 里面写上lin。像下面这样

    //let videoObj = Video() remove this line and add it inside for loop
    var tempVideos = [Video] ()
    
    //loops through all items in videoJson and assigns each value
    for (_, value):(String, JSON) in videoJSON["items"] {
        // here you have to create new object of Video for every iteration
        let videoObj = Video()
    
        videoObj.videoId = value["id"].stringValue
        videoObj.videoTitle = value["snippet"]["title"].stringValue
        videoObj.videoDescription = value["snippet"]["description"].stringValue
        videoObj.videoThumnailUrl = value["snippet"]["thumbnails"]["standard"]["url"].stringValue
    
        //prints different title & img url
        //print("title ",videoObj.videoTitle)
        print("thmbUrl" ,videoObj.videoThumnailUrl)
        tempVideos.append(videoObj)
    }
    

    【讨论】:

    • 天啊!!!谢谢你,非常感谢你。我已经连续工作了三天,你刚刚解决了它。谢谢你
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-11-19
    • 1970-01-01
    • 2014-10-26
    • 1970-01-01
    • 2018-02-19
    • 2017-09-23
    相关资源
    最近更新 更多