未调用 TableViewCell 内的 UICollectionView [英] UICollectionView inside TableViewCell not called

查看:13
本文介绍了未调用 TableViewCell 内的 UICollectionView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 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]

推荐答案

不确定,这是否可以解决您的问题,但是花了这么多时间终于找到了答案,为什么CollectionView delgate 方法在嵌入 TableView 时不会被调用:

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

遵循这个(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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆