如何解决从 collectionView 单元格中获取和更新 UITableView 单元格单击
比如说,我有一个带有 tableView
的 ViewControllerA,并且 tableView
使用 xib
方法创建和加载了它的标题视图,并且那个 headerView 包含一个 .horizontal
滚动方向 collectionView,我的问题是如何在 headerView 内的 tableView
发起的点击中更新我的 collectionView
的内容?
比如说,我点击了 Tableview 的 HeaderView 内 UICollectionViewCell
上的“Research”标签,tableView 重新加载了“Research”的数据,接下来我点击了“Design”标签,tableView 重新加载了自身和显示“设计”的数据......这样的事情可能吗?我被困在这里...
解决方法
将标头 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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。