gpt4 book ai didi

arrays - Swift 中带有两个数组的 MultipleCollectionViews

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

目前我正在解析 Json 中的数据并将其保存在本地。然后我用第一个数组中的数据填充 CollectionView。但现在我想添加第二个 CollectionView,它取决于第一个 CollectionView 中的选择。

我的 Json 结构:

struct Posts: Codable {
let id: Int
let name: String
let ImageURL: String
let Rarity: [PostType]
}

struct PostType: Codable {
let type: String
let test: int
let test2: String
}

我尝试过的:

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if collectionView == self.PostsCollectionView{
return getPostsFromDisk().count
}else
{
return feedWrapper.count //not working

}

}
var feedWrapper:[PostType] = []

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if collectionView == self.PostsCollectionView{
let cell = PostsCollectionView.dequeueReusableCell(withReuseIdentifier: "PostsUICollectionViewCell", for: indexPath) as! PostsUICollectionViewCell

cell.PostsNameLbl.text = getPostsFromDisk()[indexPath.row].name
cell.PostsImage.sd_setImage(with: URL(string: getPostsFromDisk()[indexPath.row].ImageURL))

return cell
}else
{
let cell = PostsRareCollectionView.dequeueReusableCell(withReuseIdentifier: "PostsRareCollectionViewCell", for: indexPath) as! PostsRareCollectionViewCell

if(feedWrapper[indexPath.row].type == "normal"){
cell.PostsRareImageView.image = [imageFromAssets]
}

return cell
}


}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
PostsName.text = getPostsFromDisk()[indexPath.row].name

//thougt i could save the selecte array to feedWrapper but its not working like expected
feedWrapper = getPostsFromDisk()[indexPath.row].Rarity


}

我认为我的 var feedWrapper 一开始没有填满,但我不知道该怎么做

我希望有人能帮我解决这个问题

提前致谢

最佳答案

您需要重新加载collectionView。您可以使用此代码:

使用 didset 声明您的变量(假设您的数组位于 View Controller 内部)

    var feedWrapper:[PostType] = [] {
didSet {
self.yourCollectionView.reloadData()
}
}

关于arrays - Swift 中带有两个数组的 MultipleCollectionViews,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49520236/

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