ios - 更新 Collection View 项目

标签 ios swift xcode uitableview uicollectionview

我在我的tableViewCell 中插入了一个collectionViewTableview 包含类别列表,collectionView 包含所有产品。如何根据选择的 TableView 行在 collectionView 中拥有不同数量的项目?我尝试存储选定的 TableView 行并使用它来定义要返回的项目数,但是它要么崩溃且没有错误代码,要么告诉我该值为nil,或者只是不显示任何内容collectionView 中的 citems。任何帮助将不胜感激。感谢您的时间。

下面是我的代码:

我的自定义表格 View 单元格:

extension ExpandableCell: UICollectionViewDelegate, UICollectionViewDataSource {




    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

    let toReturn = categoryItems.count

        return counter
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        //
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionCell", for: indexPath) as! CustomCollectionViewCell
        //What is this CustomCollectionCell? Create a CustomCell with SubClass of UICollectionViewCell
        //Load images w.r.t IndexPath
        print(self.selectedCategory.description)
        let newArray = starbucksMenu[selectedCategory]
        //cell.image.image = UIImage(named: (allItems[selectedCategory]?[indexPath.row])!)
        cell.label.text = categoryItems[indexPath.row]
        //cell.layer.borderWidth = 0.1




        return cell
    }

我的 TableView 委托(delegate)方法:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print(indexPath.row)
        word = indexPath.row
        guard let cell = tableView.cellForRow(at: indexPath) as? ExpandableCell
            else { return }

        switch cell.isExpanded
        {
        case true:
            self.expandedRows.remove(indexPath.row)
            self.selectedCategory = ""
        case false:
            self.expandedRows.insert(indexPath.row)

        }



        self.selectedCategory = categories[indexPath.row]
        print(self.selectedCategory)
        //self.array = starbucksMenu[starbucksMenuCategories[indexPath.row]]!
        //self.collectionView.reloadData()
        cell.menuItems = allItems[selectedCategory]!
        cell.categoryItems = allItems[selectedCategory]!
        cell.isExpanded = !cell.isExpanded
        self.itemsArray = allItems[selectedCategory]!
        self.tableView.beginUpdates()
        self.tableView.endUpdates()




        }

我尝试了很多事情,我尝试将项目添加到数组中并返回计数(不显示任何内容)。我有一本包含必要项目的字典,所以我也尝试返回 allItems[selectedCategory]?.count ,这总是返回一个错误,我相信 selectedCategory 一旦被调用就没有任何值(value)。

最佳答案

在 beginUpdate() 和 endUpdate() 之间使用适当的操作创建 Collection View 项的 for 循环

关于ios - 更新 Collection View 项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43865470/

相关文章:

ios - xcode 在运行时报告约束错误 - 但找不到它?

iphone - Xcode 目标名称为空 - 无法更改

ios - 当应用程序包中包含大量图像时,性能会受到影响

iphone - 添加 tableview 作为 subview (如果你必须从 UITableVC 继承)

ios - 删除AVAudioSessionCategoryPlayback实例

ios - 无法从 TimeZone.abbreviation() 初始化 TimeZone

xcode - 如何使用变量来构造命令

ios - xcode 8.2 中显示 'RCTLogLevel' 错误的重新定义

ios - 如何在 NSMutableArray 中对对象进行分组并在 Objective-C 中划分为单独的 NSMutableArray

ios - 是否可以使用 Swift 进行 VIPER 架构设计?