Search code examples
swiftuitableviewuicollectionviewuicollectionviewcell

Populating tableviews with data in collection view


I need to populate data in a lot of tableviews that are in a collection view. The amount of tableviews is growing with the data coming in, which is unknown. Every tableview is in uicollectionview cell. Swiping horizontally. Currently data is being displayed in the first table and in third, and fifth and so on. And all data is the same. I want the data to only be displayed in first. And when I swipe to next table, the table displays the new data there.

Edit:

I can display data in every other tableview. And move between tableviews with the data still present, which is what I want.

It skips over every other tableview and displays data, and I don't know why!

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

Update:

There is also something weird happening with the indexPath.item: When I am at the first cell, it print indexPath.item = 0, when I am in the middle cell it display that it indexPath.item = 2.

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

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

Update

Solved above gif problem with:

collectionView.isPrefetchingEnabled = false

Now the data is displayed correctly in the first tableview, and when I switch to next tableview the new data is displayed there, but then it duplicates so the 3rd is the same as 1st and 4th is the same as 2nd tableview.

Update with Code: MenuBar(Top Uicollectionview with cells, the circled 1,2,3..etc images)

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

Solution

  • You could accomplish this by setting each cell as its own tableView's 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
         }
    
    }