簡體   English   中英

從集合中獲取和更新 UITableView 單元格查看單元格單擊

[英]fetch & update UITableView cells from a collectionView cell click

說,我有一個帶有tableView的 ViewControllerA ,並且該tableView具有使用xib方法創建和加載的 header 視圖,並且該 headerView 包含一個.horizontal滾動方向 collectionView,我的問題是如何在單擊啟動時更新我的tableView的內容通過 headerView 內的collectionView

類似的例子如下圖所示: 在此處輸入圖像描述

比如說,我在 Tableview 的UICollectionViewCell內的 UICollectionViewCell 上點擊了“Research” label,tableView 重新加載了“Research”的數據,然后我點擊了“Design”label,tableView 重新加載了自身並顯示了“Design”的數據。 . 這樣的事情可能嗎? 我被困在這里……

將 header UICollectionView 鏈接到UICollectionView中的插座,然后實現所有必需的委托方法。 添加此委托方法:

class ViewController: UIViewController{

var categories = [String]()
var discoverData = [discoverModel]()
var tableView: UITableView!
override func viewDidLoad() {
    super.viewDidLoad()
}

}

extension ViewController: UICollectionViewDelegate, UICollectionViewDataSource{

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    categories.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    //code to initialize cells of header collecionView
}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    let selectedCategory = categories[indexPath.row]
    let cellToReload = discoverData.filter{return $0.serviceName == selectedCategory}[0]
    
    guard let indexPathsOfCellToRealod = discoverData.firstIndex(of: cellToReload) else { return }
    tableView.reloadRows(at: [IndexPath(index: indexPathsOfCellToRealod)], with: .automatic)
}
}

struct discoverModel: Equatable{
   var image: UIImage
   var serviceName: String
   var name: String
   var description: String
}

要完成這項工作,只需將標准UITableView委托方法添加到您的 ViewController

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM