TableViewCell内部的UICollectionView不被调用 [英] UICollectionView inside TableViewCell not called
问题描述
我有一个UITableView在这样的每个单元格中包含一个水平滚动UICollectionView
I have a UITableView contains one horizontal scroll UICollectionView in every cell like this
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! ServiceTypeCell
cell.collectionView.delegate = self
cell.collectionView.dataSource = self
cell.lblName.text = serviceTypes[indexPath.row].name
cell.tag = getTag(indexPath)
print("uitableviewcell : \(indexPath)")
return cell
}
在每个UICollectionViews中
And in every UICollectionViews
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
let cell = collectionView.superview?.superview as! ServiceTypeCell
let indexPath = getIndexPath(cell.tag)
print("uitableviewcell from uicollectionview : \(indexPath)")
return serviceTypes[indexPath.row].services.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let tableViewCell = collectionView.superview?.superview as! ServiceTypeCell
let tableViewIndexPath = getIndexPath(tableViewCell.tag)
let service = serviceTypes[tableViewIndexPath.row].services[indexPath.row]
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! ServiceTypeDetailCell
cell.lblName.text = service.name
return cell
}
我的问题是UICollectionView数据源仅在UITableViewCell索引0到3之间调用.为什么会发生这种情况?
My problem is UICollectionView Datasource only called between UITableViewCell index 0 to 3. Why does this happen?
我的打印调试结果是这样的
My print debug result like this
uitableviewcell : [0, 0]
uitableviewcell from uicollectionview : [0, 0]
uitableviewcell : [0, 1]
uitableviewcell from uicollectionview : [0, 1]
uitableviewcell : [0, 2]
uitableviewcell from uicollectionview : [0, 2]
uitableviewcell : [0, 3]
uitableviewcell from uicollectionview : [0, 3]
uitableviewcell : [0, 4]
uitableviewcell : [0, 5]
uitableviewcell : [0, 6]
推荐答案
不确定,这是否可以解决,但是最后花费了这么多时间,我终于找到了嵌入到 TableView 中的> CollectionView delgate方法不会被调用:
Not sure , whether this can Resolve your issue or not , but having consuming so much time finally i found the answer why CollectionView delgate method not gets called when it is embed inside TableView :
我们基本上使用这两个:
we basically use these two :
-
UICollectionViewDataSource
UICollectionViewDataSource
UICollectionViewDelegate
UICollectionViewDelegate
但忘记遵循以下要求: UICollectionViewDelegateFlowLayout
but forget to conform this : UICollectionViewDelegateFlowLayout
遵循此(UICollectionViewDelegateFlowLayout)确实解决了我的问题.
After conforming this (UICollectionViewDelegateFlowLayout) did solve my issue .
希望它也可以解决其他问题. 随时发表评论并分享您的反馈. 谢谢.
Hope it will solve other issue too. Feel free to comment and share your feedback. Thanks.
这篇关于TableViewCell内部的UICollectionView不被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!