ios - 从 collectionView 单元格中获取和更新 UITableView 单元格单击
问题描述
说,我有一个 ViewControllerA ,tableView
它tableView
的标题视图使用xib
方法创建和加载,并且该 headerView 包含一个.horizontal
滚动方向 collectionView,我的问题是如何更新由headerView 内部tableView
发起的点击内容?collectionView
比如说,我点击了UICollectionViewCell
Tableview的HeaderView里面的“Research”标签,tableView重新加载了“Research”的数据,接下来我点击了“Design”标签,tableView重新加载了自己并显示了“Design”的数据。类似的事情是可能的吗?我被困在这里……
解决方案
将标题链接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
推荐阅读
- vue.js - Vue Composition API 中的反应式
- c - 24 小时到 12:00 时间,进入 12:00 时出现问题,
- android - 如何解决 Android 上的点击/触摸冲突?
- python - 如何解析来自 JSON 请求的数据?
- javascript - 删除隐藏/显示库存工具 GUI 的箭头
- linux - 使用 IOMMU 的 GPU 直通
- java - 无法通过 Jenkins 管道将工件部署到 Nexus 3
- python - Brightcove API - 如何使用请求获取访问令牌?
- python - 烧瓶 CSV 到 HTML 表
- firebase - FCM 消息编写器目标用户段有 0 个用户