【问题标题】:UICollectionView inside TableViewCell not called未调用 TableViewCell 内的 UICollectionView
【发布时间】:2017-05-16 07:50:42
【问题描述】:

我有一个 UITableView 在每个单元格中都包含一个水平滚动 UICollectionView 像这样

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 中

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 之间调用。为什么会发生这种情况?

我的打印调试结果是这样的

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]

【问题讨论】:

  • 你设置UICollectionView的滚动方向是水平的吗?
  • 我想先停止tableviewDidselect。然后选择集合视图单元格。
  • @Imad 是的,我已经在情节提要中设置了水平滚动
  • @PhaniRaghu 停止 tableviewDidSelect 是什么意思?这个tableview不需要任何选择
  • 对不起,我误解了这个问题。

标签: ios swift uitableview uicollectionview


【解决方案1】:

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

我们基本上使用这两个:

  • UICollectionViewDataSource

  • UICollectionViewDelegate

但忘记遵守这个:UICollectionViewDelegateFlowLayout

遵循这个(UICollectionViewDelegateFlowLayout)确实解决了我的问题。

希望它也能解决其他问题。 随时发表评论并分享您的反馈。 谢谢。

【讨论】:

    【解决方案2】:

    发生这种情况是因为您的集合视图实例与单元格一起重复使用。 仅创建和重用了 4 个实例。

    您可以在自定义 tableview 单元格中编写您的 collectionview 逻辑,并将自定义单元格作为 collectionview 的委托和数据源。

    【讨论】:

    • 是的,我知道,它只发生在 iPhone5 中。你知道如何解决这个问题吗?
    • 在 ServiceTypeCell 单元格中编写集合视图数据源和委托方法,并将委托和数据源作为单元格分配给集合视图。还要在单元格中创建一个“服务”属性并将数据分配给 cellForRowAtIndexpath 中的单元格
    • 我尝试将 uicollectionview 数据源移动到 ServiceTypeCell 类中,仍然得到相同的结果。在我的 uitableviewcell cellForRowAtIndexpath 中: cell.serviceType = serviceTypes[indexPath.row] cell.collectionView.delegate = cell cell.collectionView.dataSource = cell
    • 我得到的唯一解决方案是在 cellForRowAtIndexpath 中设置 cell.collectionView.reloadData() 但如果我向上滚动并返回此单元格,它将再次将单元格的位置重置为 0
    【解决方案3】:

    在单元类上编写集合视图代码。并将collectionview委托给cell,因为cell是collection view的超类。检查此屏幕截图

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多