如何更新表视图中集合视图中的数据?

How to update data in a collectionview within a tableview?

嗨,现在我在 UITableViewCell 中有一个 UICollectionViewUICollectionView 充当为每个单独的单元格放置标签的位置。当我不是 searching/filtering UITableViewCells 时,它目前显示正常,但是当我搜索时,代码正在过滤 UITableViewCells,UICollectionView 不会用它过滤。

我的意思是:

未过滤时单元格的外观(每个项目符号点都是 UITableViewCell):

过滤时单元格的外观:

如您所见,标签对于它们各自的索引行保持不变,而不是更改为应有的样子——它们过滤后的索引行。

这是我的代码——我的编程方式有点像通过漏洞(将数据存储在 UITableViewCell 中的隐藏标签中),所以这可能是问题的根源,尽管我我不确定。

mainViewController:

class SearchViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate {

     var items: [ItemsModel] = []
     var filteredItems: [ItemsModel] = []
     var searching = false
     var delegate: controlsReloadingCollectionView?

     public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell", for: indexPath) as! TableViewCell
        var tags = ""
        if searching {
            cell.nameLabel.text = filteredItems[indexPath.row].name
            cell.infoLabel.text = "## mi  \u{2022}  \(filteredItems[indexPath.row].Age ?? "") age  \u{2022}  \(filteredItems[indexPath.row].StartTime ?? "") "
            
            tags = filteredItems[indexPath.row].Tags!
            cell.tagsModelLabel.text = tags
        }
        else {
            cell.nameLabel.text = items[indexPath.row].name
            cell.infoLabel.text = "## mi  \u{2022}  \(Items[indexPath.row].Age ?? "") age  \u{2022}  \(Items[indexPath.row].StartTime ?? "") "
            tags = items[indexPath.row].Tags!
            cell.tagsModelLabel.text = tags                   
        }
        return cell
    }
    
  func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
   
       filteredItems = items.filter({ItemsModel -> Bool in
           guard let text = searchBar.text else { return false }
        return itemsModel.itemName!.lowercased().contains(text.lowercased())
       })
        searching = true
        SearchItemListTableView.reloadData()
        self.delegate?.reloadCollectionView()   
    }

}
    

在 TableViewCell 上:

protocol controlsReloadingCollectionView {
    func reloadCollectionView()
} // I used this so I could call this function in the mainViewController.

class TableViewCell: UITableViewCell, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, controlsReloadingCollectionView {


      @IBOutlet var itemNameLabel: UILabel!
      @IBOutlet weak var infoLabel: UILabel!
      @IBOutlet weak var TagsCollectionView: UICollectionView!
      var tagsCollectionViewArray = [String]()

     override func awakeFromNib() {
        super.awakeFromNib()
        
        TagsCollectionView.delegate = self
        TagsCollectionView.dataSource = self
        TagsCollectionView.reloadData()  
    }

 func reloadCollectionView() {
        TagsCollectionView.reloadData()
    }

    func setTagsCollectionViewArray() {
            tagsCollectionViewArray = tagsModelLabel.text?.components(separatedBy: " ") ?? ["didn't work"]
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        if tagsModelLabel.text == "" {
            return 0
        }
        else {
            setTagsCollectionViewArray()
            return tagsCollectionViewArray.count
        }  
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "SearchTagsCollectionViewCell", for: indexPath) as! TagsCollectionViewCell
     
        setTagsCollectionViewArray()
        cell.tagsLabel.text = tagsCollectionViewArray[indexPath.row] 
        cell.tagsLabel.sizeToFit()
        return cell
    }
    
}

有谁知道为什么它不起作用?我尝试了不同的方法,例如为过滤的项目分配不同的变量和不同的隐藏标签,但似乎对我没有任何作用。当我 运行 代码时,隐藏的标签实际上在我搜索时正确显示,但是,只是我无法在 TableViewCell 端获得任何变量以正确更新。

您可以像下面这样简单地制作:

在 mainViewController 上:

 var items: [ItemsModel] = []
 var filteredItems: [ItemsModel] = []
 var searching = false
 var delegate: controlsReloadingCollectionView?

 public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell", for: indexPath) as! TableViewCell
    var tags = ""
    if searching {
        cell.nameLabel.text = filteredItems[indexPath.row].name
        cell.infoLabel.text = "## mi  \u{2022}  \(filteredItems[indexPath.row].Age ?? "") age  \u{2022}  \(filteredItems[indexPath.row].StartTime ?? "") "
        
        tags = filteredItems[indexPath.row].Tags!
        cell.tagsModelLabel.text = tags
        cell.tagsCollectionViewArray = yourDataSourceArray //Array which you want to display in collectionView
         
    } else {
        cell.nameLabel.text = items[indexPath.row].name
        cell.infoLabel.text = "## mi  \u{2022}  \(Items[indexPath.row].Age ?? "") age  \u{2022}  \(Items[indexPath.row].StartTime ?? "") "
        tags = items[indexPath.row].Tags!
        cell.tagsModelLabel.text = tags
        cell.tagsCollectionViewArray = yourDataSourceArray //Array which you want to display in collectionView                   
    }
    
    cell.yourCollectionView.reloadData() //Reload your collectionView
    return cell
}

调用您的 API(如果您使用)后,只需重新加载您的 tableView 即可获得 API 和

的成功结果

yourTable.reloadData()

它工作得很好。