如何在具有唯一数据源的每个 UI 表单视图单元格中放置不同的 UITableView



>我标记了 5 个表视图(每个视图都有一个唯一的数据源(,并将它们放置在添加到 UICollectionView 的相同数量的 UICollectionCells 中。

目标是让用户水平轻扫,并显示每个表视图和一组唯一的数据。以下代码工作正常,但数据在第三次滑动后开始重复。

看起来每个表视图都cellForItemAt中正确标记,我可以看到它们显示在cellForRowAt(分配了唯一数据源的位置(中,直到第四个表视图。

有趣的是,如果我向上滑动被推离屏幕的单元格,就会出现正确的数据。表视图是否需要重新加载?还是收藏视图?

什么会导致只有前三个表视图正常工作?我是 swift 的新手,所以我希望这很简单。

import UIKit
class CollectionViewCell: UICollectionViewCell {
    let reuseIdentifier1 = "reuseIdentifier1"
    var tableView: UITableView = UITableView()
    var array0 = ["0", "0", "0", "0", "0"]
    var array1 = ["1", "1", "1", "1", "1"]
    var array2 = ["2", "2", "2", "2", "2"]
    var array3 = ["3", "3", "3", "3", "3"]
    var array4 = ["4", "4", "4", "4", "4"]
    override init(frame: CGRect) {
        super.init(frame: frame)
        tableView.delegate = self
        tableView.dataSource = self
        tableView.translatesAutoresizingMaskIntoConstraints = false
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: reuseIdentifier1)
        setupTableView()
    }
    func setupTableView() {
        addSubview(tableView)
        tableView.topAnchor.constraint(equalTo: topAnchor).isActive = true
        tableView.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
        tableView.leftAnchor.constraint(equalTo: leftAnchor).isActive = true
        tableView.rightAnchor.constraint(equalTo: rightAnchor).isActive = true
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
extension CollectionViewCell: UITableViewDelegate, UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 5
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: reuseIdentifier1, for: indexPath)
        // CHANGE TABLE DATA DEPENDING ON TAG
        if tableView.tag == 0 {
            cell.textLabel?.text = array0[indexPath.item]
        }
        if tableView.tag == 1 {
            cell.textLabel?.text = array1[indexPath.item]
        }
        if tableView.tag == 2 {
            cell.textLabel?.text = array2[indexPath.item]
        }
        if tableView.tag == 3 {
            cell.textLabel?.text = array3[indexPath.item]
        }
        if tableView.tag == 4 {
            cell.textLabel?.text = array4[indexPath.item]
        }
        return cell
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 50
    }
}
class ViewController: UIViewController {
    let reuseIdentifier2 = "cell"
    let theCollectionView:UICollectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: UICollectionViewFlowLayout.init())
    let theCollectionViewLayout:UICollectionViewFlowLayout = UICollectionViewFlowLayout.init()
    override func viewDidLoad() {
        super.viewDidLoad()
        theCollectionView.frame = .zero
        theCollectionView.backgroundColor = UIColor.clear
        theCollectionView.dataSource = self
        theCollectionView.delegate = self
        theCollectionViewLayout.scrollDirection = .horizontal
        theCollectionViewLayout.minimumLineSpacing = 0.0
        theCollectionViewLayout.minimumInteritemSpacing = 0.0
        theCollectionViewLayout.estimatedItemSize = CGSize(width: view.frame.width, height: view.frame.height)
        theCollectionView.collectionViewLayout = theCollectionViewLayout
        theCollectionView.isPagingEnabled = true
        theCollectionView.register(CollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier2)
        theCollectionView.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(theCollectionView)
        theCollectionView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
        theCollectionView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
        theCollectionView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
        theCollectionView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
    }
}
extension ViewController: UICollectionViewDelegateFlowLayout, UICollectionViewDelegate, UICollectionViewDataSource {
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 5
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier2, for: indexPath) as! CollectionViewCell
        // TAG THE TABLEVIEWS HERE
        cell.tableView.tag = indexPath.row
        return cell
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: theCollectionView.frame.width, height: theCollectionView.frame.height)
    }
}
当您

在表视图的cellForRowAt中说if tableView.tag ==时,您假设此表视图的标记已在集合视图的cellForItemAt中更改。但你不知道这是事情发生的顺序,所以这是一个冒险的假设——事实上,它显然是行不通的。

您可以通过一些断点或日志记录轻松确认这一点,以在滑动时显示事情发生的顺序。

此外,请注意,集合视图项 0 中的表视图可能会在项 3 中重复使用。但是,一旦表视图具有新标记,您将需要重新加载它,以使其具有新的单元格值。否则,您只会继续看到第 0 项中的单元格(显然正是发生在您身上的事情(。

好的......就是这样。我只是在cellForItemAt return cell之前添加了以下行。

cell.tableView.reloadData()

它有效!

最新更新