gpt4 book ai didi

ios - 禁用滚动时,UICollectionView reloadData不起作用

转载 作者:行者123 更新时间:2023-11-30 11:16:40 25 4
gpt4 key购买 nike

在我的collectionView单元内部,我有一个嵌套的集合视图。会在进行api调用(didSet)之后首先进行更新,但是在尝试将数据附加到数据源数组并重新加载collectionView时,采用了加载更多机制。禁用滚动时,更改不会反映出来。一旦启用滚动,它将开始正常工作。禁用滚动时如何使其工作,以便显示附加的项目。这是我的代码:

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 = ["code": code!, "userID": userID!,
"plateID": platesID!]

network.makeGetRequestDecodable(parameters: parameters, url:
"get/getGridPS.php") { (reponse) in


do {


let modeled = try JSONDecoder().decode([gps_sing].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 = ["code": code!, "userID": userID!,
"plateID": platesID!,"token":"loadScroll","lim":gps_singleton_posts!
[(gps_singleton_posts!.count)-1].id!]


network.makeGetRequestDecodable(parameters: parameters, url: "get/getGridPS.php") { (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")
}
}


感谢您的回答!

最佳答案

无需禁用滚动,则不应这样做。有一些API可以仅重载所需的UICollectionView部分,而不会影响其余部分。您只需要对集合进行正确的设置。

1个装弹区

https://developer.apple.com/documentation/uikit/uicollectionview/1618092-reloadsections

2重新加载单个项目(单元)

https://developer.apple.com/documentation/uikit/uicollectionview/1618055-reloaditems

3进阶方式

有一篇非常不错的文章,介绍如何精细地更新您的收藏夹视图https://medium.com/flawless-app-stories/a-better-way-to-update-uicollectionview-data-in-swift-with-diff-framework-924db158db86

关于ios - 禁用滚动时,UICollectionView reloadData不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51682167/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com