ios - Swift 数组追加不适用于 collectionView 重新加载数据

标签 ios swift uicollectionview uicollectionviewcell

在我的 collectionView 单元格中,我有一个嵌套的 Collection View 。它在进行 api 调用 (didSet) 后首次更新,但在加载更多机制中,当我尝试将数据附加到数据源数组并重新加载 collectionView.. 更改未反射(reflect)出来。这是我的代码

class PhotosCollectionViewController:    

   UICollectionViewCell,UICollectionViewDelegate,
   UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {


let network = networkingService()
let keychain = KeychainSwift()

var userID: String?
var code: String?

 var platesID: String!{
    
    didSet{
        
         let parameters: Parameters = [:]
        
        network.makeGetRequestDecodable(parameters: parameters, url: 
         "endpoint") { (reponse) in
            
            
            do {
                
                
                let modeled = try JSONDecoder().decode([Model].self, from:reponse)
                
                self.gps_singleton_posts = modeled
                print("Reload Method Called")
                self.appsCollectionView?.reloadData()
                
                
            } catch let err {
                print(err)
            }
            
            
        }
        
        
    }
    
}
 var gps_singleton_posts: [gps_sing]?
let gridId = "gridID"
var appsCollectionView: UICollectionView? = {
    
    let layout = UICollectionViewFlowLayout()
    layout.minimumInteritemSpacing = 0
    layout.minimumLineSpacing = 3

    
    let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
    collectionView.backgroundColor = UIColor.white

    collectionView.translatesAutoresizingMaskIntoConstraints = false
    return collectionView
    
}()

override init(frame: CGRect) {
    
    super.init(frame: frame)
    
    
    
    
    
    if let value = keychain.get("userID") {
        self.userID = value
        // print("keychain\(self.userID!)")
    }
    if let value = keychain.get("security_token") {
        self.code = value
        // print("keychain\(self.code!)")
    }
    
   
    appsCollectionView?.dataSource = self
    appsCollectionView?.delegate = self
    appsCollectionView?.contentInset = UIEdgeInsetsMake(60, 0, 0, 0)
     appsCollectionView?.scrollIndicatorInsets =  UIEdgeInsetsMake(60, 
     0, 0, 0)
    
    addSubview(appsCollectionView!);
    
    
    addConstraintsWithFormat("H:|[v0]|", views: appsCollectionView!)
    addConstraintsWithFormat("V:|[v0]|", views: appsCollectionView!)
    appsCollectionView?.register(ImageCell.self, 
    forCellWithReuseIdentifier: gridId)
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}



func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
   
    return CGSize(width: 123, height: 123)
   
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
   
    print(gps_singleton_posts)
    if let count = gps_singleton_posts?.count{
        
        return count
    }

    return 0
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    
   
let parameters: Parameters = [:]
    
    
  network.makeGetRequestDecodable(parameters: parameters, url: "Endpoiint") { (reponse) in
        
        
        do {
            
            
            let modeled = try JSONDecoder().decode([gps_sing].self, from:reponse)
            
            for model in modeled{
                self.gps_singleton_posts?.append(model)
            }
           print("Reload Method Called")
            collectionView.reloadData()
            print(self.gps_singleton_posts)
            
        } catch let err {
            print(err)
         }
    }
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    
    print(self.gps_singleton_posts?.count)
    
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: gridId, for: indexPath)
        as! ImageCell
    
    cell.backgroundColor = UIColor.red
    cell.singlePost = gps_singleton_posts![indexPath.row]
    
    return cell
 
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
    
   
    scrollView.isScrollEnabled = false
}


 }


 class ImageCell: UICollectionViewCell {


var imageViewGrid: UIImageView = {
    let imageView = UIImageView()
    imageView.contentMode = .scaleAspectFill
    imageView.frame = CGRect(x: 0, y:0 , width: 100, height: 100)
    imageView.image = UIImage(named: "delete")
    imageView.clipsToBounds = true
    imageView.translatesAutoresizingMaskIntoConstraints = false
    return imageView
}()

var textLabel: UILabel = {
    
    let lv = UILabel()
    lv.textColor = UIColor.red
    lv.translatesAutoresizingMaskIntoConstraints = false
    return lv
}()

var singlePost: gps_sing! {
    
    didSet{
        
        let imageUrl = singlePost.uid
        print(imageUrl!)
        
        imageViewGrid.setImageWith(NSURL(string: imageUrl!)! as URL)
        
        
    }
    
}

override init(frame: CGRect) {
    super.init(frame: frame)
    setUpViews()
}

func setUpViews(){
    
    addSubview(imageViewGrid)
    addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[v0]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": imageViewGrid]))
    
    
    addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[v0]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": imageViewGrid]))
    
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
} 
  }

在上面的代码中 (didSelectAt) 函数用于加载更多项目..但是没有反射(reflect)出变化。请帮帮我。非常感谢您回答我的问题。

最佳答案

所以,我找到了解决问题的方法。在上面的 Collection View 中,我禁用了滚动..因为重新加载数据后添加的内容没有显示出来。一旦我启用滚动数据开始显示。

关于ios - Swift 数组追加不适用于 collectionView 重新加载数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51673406/

相关文章:

swift - 如何设置单元格间距和 UICollectionView - UICollectionViewFlowLayout 大小比例?

ios - 如何在自定义键盘中输入自定义字体作为输入

ios - heightForRowAtIndexPath 无法正常工作

swift - 比较 NSIndexPath Swift

ios - 沿弧定位 CATextLayers

ios - 在 UIStackview 中添加时未在 UICollectionView 中触发方法

objective-c - 如何以编程方式在日历中以与月份和星期相匹配的方式布局日期?

iphone - 在 iOS 方法内部,未设置字典的值

iphone - 从前置摄像头 iOS 5.0 捕获时总是看到镜像

ios - 文本更改时 UILabel 文本颜色更改