英文:
How to show a specific array value first in UICollectionView when scrolling horizontally in Swift?
问题
我正在从JSON中获取数组,以在CollectionView上显示数据。
JSON数据结构如下:
{
    id = 9;
    image = "1685696735-5869.jpg";
    "user_id" = 13;
},
{
    id = 17;
    image = "1685709043-8696.jpg";
    "user_id" = 13;
},....等等
我现在获取了selectedID和selectedImage的值,现在我需要在CollectionView中首先显示所选的ID数组值。
例如,如果selectedImage = 1685709043-8696.jpg,那么我需要在CollectionView的第一个单元格中首先显示image = "1685709043-8696.jpg",如何做到这一点?
代码: 如果getImagesData?.result?.userToGallery或selectedDataArray包含**selectedImage**,那么该单元格应首先显示在imageCollectionView中,其余的应水平滚动显示。
class AllGalleryImgViewController: UIViewController {
    
   private var getImagesData = GetGalleryImageModel(dictionary: NSDictionary()){
        didSet{
            
            self.selectedDataArray = getImagesData?.result?.userToGallery ?? []
            imagecollectionView.reloadData()
        }
    }
    
    public var selectedDataArray = [UserToGallery]()
    
    @IBOutlet weak var imagecollectionView: UICollectionView!
    var selectedImage: String!
    var selectedID: Int!
    var imagesArray = [String]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        getImagesServiceCall()
    }
    
    func getImagesServiceCall() {
        
        APIReqeustManager.sharedInstance.serviceCall(param: nil, method: .get, url: CommonUrl.my_gallery_image){ [weak self] (resp) in
            
            self?.getImagesData = GetGalleryImageModel(dictionary: resp.dict as NSDictionary? ?? NSDictionary())
        }
    }
}
extension AllGalleryImgViewController: UICollectionViewDelegate, UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return getImagesData?.result?.userToGallery?.count ?? 0
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "AllBigGalleryCollectionCell", for: indexPath) as! AllBigGalleryCollectionCell
        
        cell.galleryImageView.getImage(withImagePath: getImagesData?.result?.userToGallery?[indexPath.item].image ?? "")
        
        return cell
    }
}
如何过滤数组并在CollectionView的第一个单元格中显示该值,当水平滚动时应显示其余的值。
英文:
I am getting Array from JSON to show data on collectionview
JSON data structure is like this:
            {
                id = 9;
                image = "1685696735-5869.jpg";
                "user_id" = 13;
            },
            {
                id = 17;
                image = "1685709043-8696.jpg";
                "user_id" = 13;
            },.... so on
i am getting selectedID and selectedImage value now i need to show that selectedID array value first in the collectionview
for eg selectedImage = 1685709043-8696.jpg then i need to show image = "1685709043-8696.jpg" first in collectionview first cell how to do that
code: here if getImagesData?.result?.userToGallery or selectedDataArray contains selectedImage then that cell should show first in imagecollectionView and remaining should scroll horizontally
class AllGalleryImgViewController: UIViewController {
    
   private var getImagesData = GetGalleryImageModel(dictionary: NSDictionary()){
        didSet{
            
            self.selectedDataArray = getImagesData?.result?.userToGallery ?? []
            imagecollectionView.reloadData()
        }
    }
    
    public var selectedDataArray = [UserToGallery]()
    
    @IBOutlet weak var imagecollectionView: UICollectionView!
    var selectedImage: String!
    var selectedID: Int!
    var imagesArray = [String]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        getImagesServiceCall()
    }
    func getImagesServiceCall() {
        
        APIReqeustManager.sharedInstance.serviceCall(param: nil, method: .get, url: CommonUrl.my_gallery_image){ [weak self] (resp) in
            
            self?.getImagesData = GetGalleryImageModel(dictionary: resp.dict as NSDictionary? ?? NSDictionary())
        }
    }
    }
    extension AllGalleryImgViewController: UICollectionViewDelegate, UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return getImagesData?.result?.userToGallery?.count ?? 0 //10
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "AllBigGalleryCollectionCell", for: indexPath) as! AllBigGalleryCollectionCell
        
        cell.galleryImageView.getImage(withImagePath: getImagesData?.result?.userToGallery?[indexPath.item].image ?? "")
        
        return cell
    }
}
How to filer array and show that value in collectionview first cell
i need to show filtered image first in cell.galleryImageView.getImage(withImagePath: getImagesData?.result?.userToGallery?[indexPath.item].image ?? "") and remaining should show next when scrolling horizontally
答案1
得分: 1
为了在集合视图中首先显示所选图像,请修改您的 selectedDataArray 属性以保存包含所选图像的筛选数组,如下所示:
var selectedDataArray = [UserToGallery]() {
    didSet {
        if let selectedImage = selectedImage {
            let filteredArray = selectedDataArray.filter { $0.image == selectedImage }
            let remainingArray = selectedDataArray.filter { $0.image != selectedImage }
            self.selectedDataArray = filteredArray + remainingArray
        }
    }
}
在 collectionView(_:cellForItemAt:) 方法中,使用 indexPath.item 从筛选后的数组中获取图像:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "AllBigGalleryCollectionCell", for: indexPath) as! AllBigGalleryCollectionCell
    
    cell.galleryImageView.getImage(withImagePath: selectedDataArray[indexPath.item].image)
    
    return cell
}
更新 collectionView(_:numberOfItemsInSection:) 方法以返回筛选数组的计数:
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return selectedDataArray.count
}
英文:
To display the selected image first in the collection view ,
Modify your selectedDataArray property to hold the filtered array with the selected image at the beginning,
var selectedDataArray = [UserToGallery]() {
    didSet {
        if let selectedImage = selectedImage {
            let filteredArray = selectedDataArray.filter { $0.image == selectedImage }
            let remainingArray = selectedDataArray.filter { $0.image != selectedImage }
            self.selectedDataArray = filteredArray + remainingArray
        }
    }
}
In the collectionView(_:cellForItemAt:) method, access the image from the filtered array using indexPath.item
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "AllBigGalleryCollectionCell", for: indexPath) as! AllBigGalleryCollectionCell
    
    cell.galleryImageView.getImage(withImagePath: selectedDataArray[indexPath.item].image)
    
    return cell
}
Update the collectionView(_:numberOfItemsInSection:) method to return the count of the filtered array,
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return selectedDataArray.count
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论