【问题标题】:Duplicate TableView in CollectionViewCellsCollectionViewCells 中的重复表视图
【发布时间】:2019-01-15 12:48:09
【问题描述】:

我有水平的 CollectionView,每个都有 TableView

问题

表格视图重复

代码

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: OrdersCollectionViewCell.id, for: indexPath) as! OrdersCollectionViewCell

        switch indexPath.row{
        case 0:
            cell.backgroundColor = .red
            cell.setupTableview()
            cell.tableView.backgroundColor = .red
        case 1:
            cell.backgroundColor = .green
        case 2:
            cell.backgroundColor = .blue
        case 3:
            cell.backgroundColor = .orange
        case 4:
            cell.backgroundColor = .yellow
        default:
            break
        }
        return cell
    }

setupTableview() -> 只是委托和数据源

【问题讨论】:

  • 您必须在cellForItemAt 方法中重新加载您的表格视图,因为它重用了单元格。当您只为第一个单元格设置表格时,还要为其余单元格调用删除表格方法。
  • 什么是setupTableView()?将此方法的代码添加到您的问题中

标签: ios swift uitableview uicollectionview


【解决方案1】:

请试试这个:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: OrdersCollectionViewCell.id, for: indexPath) as! OrdersCollectionViewCell

    switch indexPath.row{
    case 0:
        cell.backgroundColor = .red
        cell.setupTableview()
        cell.tableView.backgroundColor = .red
    case 1:
        cell.backgroundColor = .green
    case 2:
        cell.backgroundColor = .blue
    case 3:
        cell.backgroundColor = .orange
    case 4:
        cell.backgroundColor = .yellow
    default:
        break
    }
    cell.tableView.reloadData()
    return cell
}

它可能对您有所帮助。谢谢。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-27
    • 2019-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-03
    相关资源
    最近更新 更多