繁体   English   中英

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

[英]How to update data in a collectionview within a tableview?

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

这就是我的意思:

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

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

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

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

过滤时单元格的外观:

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

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

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

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

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
    }
    
}

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

您可以简单地制作如下:

在主视图控制器上:

 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()

它就像魅力一样工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM