【问题标题】:How to update data in a collectionview within a tableview?如何在表格视图中更新集合视图中的数据?
【发布时间】:2021-04-12 19:43:19
【问题描述】:

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

这就是我的意思:

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

  • 标题 A,描述 A,[TagA1,TagA2,TagA3]

  • 标题 B、描述 B、[TagB1]

  • 标题 C,描述 C,[TagC1,TagC2]

过滤时单元格的外观:

  • 标题 A,描述 A,[TagA1,TagA2,TagA3]

  • 标题 C、描述 C、[TagB1]

如您所见,标签在其各自的索引行中保持不变,而不是更改为应有的状态 - 过滤后的索引行。

这是我的代码——我编写它的方式是通过一个漏洞(将数据存储在 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 端获取任何变量以正确更新。

【问题讨论】:

    标签: ios arrays swift uitableview uicollectionview


    【解决方案1】:

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

    在 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(如果您使用)后,只需在 API 的成功结果中重新加载您的 tableView

    yourTable.reloadData()

    它就像魅力一样工作。

    【讨论】:

      猜你喜欢
      • 2018-07-23
      • 2019-07-08
      • 1970-01-01
      • 2018-11-16
      • 1970-01-01
      • 2016-09-05
      • 1970-01-01
      • 1970-01-01
      • 2019-05-26
      相关资源
      最近更新 更多