ios - 无法在表格视图单元格内滚动集合视图
问题描述
我看过类似的问题,但我仍然不确定到底发生了什么。我觉得它可能缺少一些简单的东西,所以如果是这样,我提前道歉。我在表格视图单元格内的集合视图没有滚动,也没有响应 didSelectItemAt方法。该项目编译正确,看起来完全符合测试的需要,但水平滚动和触摸将不起作用。另一个有趣的注意事项是表格视图在自定义单元格中响应didSelectRowAt
,但集合视图单元格没有。先感谢您。
根据类似的问题和答案,我尝试了以下方法:
cell.selectionStyle = .none
对于包含集合视图的表格单元格return nil
在具有集合视图的单元格的 tableViewswillSelectRowAt
方法中
PostViewController.swift
class PostViewController: UIViewController {
private let table: UITableView = {
let table = UITableView(frame: .zero, style: .grouped)
table.register(CustomTableCellTableViewCell.self, forCellReuseIdentifier: CustomTableCellTableViewCell.id)
table.register(UITableViewCell.self, forCellReuseIdentifier: "TableCell")
return table
}()
override func viewDidLoad() {
super.viewDidLoad()
self.view.addSubview(self.table)
self.table.delegate = self
self.table.dataSource = self
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
self.table.frame = self.view.bounds
}
}
extension PostViewController: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 170
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
switch indexPath.row {
case 2:
guard let cell = tableView.dequeueReusableCell(withIdentifier: CustomTableCellTableViewCell.id,
for: indexPath) as? CustomTableCellTableViewCell else { return UITableViewCell() }
return cell
default:
let cell = tableView.dequeueReusableCell(withIdentifier: "TableCell", for: indexPath)
return cell
}
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
}
}
** CustomTableCellTableViewCell.swift **
class CustomTableCellTableViewCell: UITableViewCell {
static let id = "CustomTableCellTableViewCell"
private let collection: UICollectionView = {
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .horizontal
layout.itemSize = CGSize(width: 128, height: 128)
let collection = UICollectionView(frame: .zero, collectionViewLayout: layout)
collection.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "CollectionCell")
collection.backgroundColor = .systemBlue
return collection
}()
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
self.addSubview(self.collection)
self.collection.delegate = self
self.collection.dataSource = self
}
required init?(coder: NSCoder) {
fatalError()
}
override func layoutSubviews() {
self.collection.frame = self.bounds
}
}
extension CustomTableCellTableViewCell: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 20
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collection.dequeueReusableCell(withReuseIdentifier: "CollectionCell", for: indexPath)
cell.backgroundColor = .systemYellow
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
print("Selected")
}
}
解决方案
添加到 UITableViewCell 的子视图位于 contentView 后面,这会阻止用户输入到达集合视图。
集合视图需要在内容视图中添加为 -
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
contentView.addSubview(self.collection)
self.collection.delegate = self
self.collection.dataSource = self
}
推荐阅读
- javascript - OpenLayers 3 捕获各种点击事件
- python - 数组上的“直接”numpy函数与numpy数组函数
- mysql - 如何检查Mysql DB中是否存在确切的slug(单次出现或多次出现)?
- spring - RenderMapping 和清除 Session 属性
- file - 解锁从 javaFX 桌面应用程序导出的 excel 文件
- spring-boot - 如何从 GitLab 存储库将 Web 应用程序部署到 AWS 实例
- puppet - 如何在 puppet 3.7 中推迟“文件”函数的执行
- c# - Rtf 图片未显示在 Gmail 中
- cluster-computing - 具有单个公共 IP 的 Apache Ignite 集群
- microsoft-cognitive - Azure Bing 语音 API 和语音转文本