![](/img/trans.png)
[英]Custom CollectionView cells' layout elements disappear when attempting to update the cell
[英]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.