swift - 使用 Collection View 中的数据填充 TableView

标签 swift uitableview uicollectionview uicollectionviewcell

我需要在 Collection View 中的大量 TableView 中填充数据。 tableviews 的数量随着数据的进来而增长,这是未知的。每个 tableview 都在 uicollectionview 单元格中。水平滑动。目前数据显示在第一个表中,第三个,第五个等等。而且所有数据都是一样的。我希望数据只显示在第一位。当我滑动到下一个表格时,该表格会在那里显示新数据。

编辑:

我可以在每隔一个 tableview 中显示数据。在数据仍然存在的 TableView 之间移动,这就是我想要的。

它跳过所有其他 tableview 并显示数据,我不知道为什么!

I can display data in the first(orange) tableview, but next data is displayed in the third(light brown), and not red.

更新:

indexPath.item 也发生了一些奇怪的事情: 当我在第一个单元格时,它打印 indexPath.item = 0,当我在中间单元格时,它显示它 indexPath.item = 2。

更新 As you can see the indexPath.item is different and all messed up compared to the MenuBar, I want them to have equal

如您所见,indexPath.item 与 MenuBar 不同,而且一团糟,我希望它们相等

更新

解决上述 gif 问题:

collectionView.isPrefetchingEnabled = false

现在数据在第一个 tableview 中正确显示,当我切换到下一个 tableview 时,新数据显示在那里,但随后它重复,因此第 3 个与第 1 个相同,第 4 个与第 2 个 tableview 相同。

使用代码更新: 菜单栏(带单元格的顶部 Uicollectionview,带圆圈的 1、2、3..etc 图像)

class MenuBar: UIView, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {

NewReceiveViewController

class NewReceiveViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout {
  var myData: [[UInt8]] = [] // will this be ReceivedArrays?
....
  override func viewDidLoad() {
    super.viewDidLoad()
  collectionView.register(ReceivedCell.self, forCellWithReuseIdentifier: cellId)

  override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! ReceivedCell

    cell.backgroundColor = UIColor.lightGray // just to see where the cell is
    cell.configure(with: myData[indexPath.row])

    return cell

 class ReceivedCell: UICollectionViewCell {
   //vars for the data
   var recievedArrays: [[UInt8]] = []  //receivedbytes array is put    into array here
   var receivedBytes: [UInt8] = []{    //receieved bytes
      didSet {
        tableView.reloadData()

      }

var data: [UInt8] = [] // will this be receieved bytes?

func configure(with data: UInt8) {

    self.data = [data]
    self.tableView.dataSource = self
}

extension ReceivedCell: UITableViewDelegate, UITableViewDataSource {

 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return recievedArrays.count //counting and returning how many receivedbytes arrays in receivedArrays
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: tableViewCell, for: indexPath)
...
// table cells set here
// I'm a bit unsure how to set up the cell with self.data
...
return cell

最佳答案

您可以通过将每个单元格设置为它自己的 tableView 的委托(delegate)来完成此操作。

class MyVC: UICollectionViewDataSource {

     var myData: [DataType]! // You MUST populate this value with your data before you start getting delegate calls. You should probably set this as soon as you initialize this view.

     func collectionView(_ collectionView: UICollectionView, cellForRowAt indexPath: IndexPath) -> UICollectionViewCell {
          // Configure collection view cell
          cell.configure(with: myData[indexPath.row]
          return cell
     }

}

class MyCell: UICollectionViewCell, UITableViewDelegate {

     @IBOutlet var tableView: UITableView!
     var data: DataType!

     func configure(with data: DataType) {
          self.data = data
          self.tableView.dataSource = self
     }

     func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
          // Set up table cell using self.data
          return cell
     }

}

关于swift - 使用 Collection View 中的数据填充 TableView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55676137/

相关文章:

swift - 使用 Swift 3.0 将 CoreData 保存到 Web 服务器

ios - 在 Swift 的 UITableView 部分获取行的 [NSIndexPath] 数组

ios - 在 UITableView 中滚动时图像变得困惑

ios - 在 SpriteKit 中预加载纹理

swift - 如何将第一个表格 View 单元格设为静态,然后使用 swift 从第二个单元格加载数据

ios - SKLabelNode 未出现在实际设备上

ios - 以编程方式更改 UITableViewStyle 和单元格 - 在 Storyboard中制作,在代码中修改

swift - 发件人 : UIImageView ! == CollectionViewCell 中的 UIImageView

ios - UIViewController里面CollectionViewCell内存问题

ios - 如何处理部分瀑布布局CollectionView