首页 > 解决方案 > CollectionViewCells 中的重复表视图

问题描述

我每个都有水平 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()-> 只是委托和数据源

在此处输入图像描述 在此处输入图像描述 在此处输入图像描述 在此处输入图像描述

标签: iosswiftuitableviewuicollectionview

解决方案


请试试这个:

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
}

它可能会帮助你。谢谢你。


推荐阅读