Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/gwt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 使用集合视图中的数据填充表视图_Swift_Uitableview_Uicollectionview_Uicollectionviewcell - Fatal编程技术网

Swift 使用集合视图中的数据填充表视图

Swift 使用集合视图中的数据填充表视图,swift,uitableview,uicollectionview,uicollectionviewcell,Swift,Uitableview,Uicollectionview,Uicollectionviewcell,我需要在集合视图中的许多TableView中填充数据。表视图的数量随着数据的进入而增加,这是未知的。每个tableview都位于uicollectionview单元格中。水平滑动。当前数据显示在第一个表、第三个表和第五个表中,依此类推。所有数据都是一样的。我希望数据只在第一个窗口中显示。当我滑动到下一个表时,该表将在那里显示新数据 编辑: 我可以每隔一段时间在表格视图中显示数据。并在数据仍然存在的情况下在表视图之间移动,这正是我想要的 它跳过其他所有的tableview并显示数据,我不知道为什么

我需要在集合视图中的许多TableView中填充数据。表视图的数量随着数据的进入而增加,这是未知的。每个tableview都位于uicollectionview单元格中。水平滑动。当前数据显示在第一个表、第三个表和第五个表中,依此类推。所有数据都是一样的。我希望数据只在第一个窗口中显示。当我滑动到下一个表时,该表将在那里显示新数据

编辑:

我可以每隔一段时间在表格视图中显示数据。并在数据仍然存在的情况下在表视图之间移动,这正是我想要的

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

更新:

indexPath.item也发生了一些奇怪的事情: 当我在第一个单元格上时,它打印了索引路径.It=0,当我在中间的单元格中时,它显示了它的索引路径.It= 2。p> 更新

正如您所看到的,与菜单栏相比,indexath.item是不同的,并且都是混乱的,我希望它们具有相同的

更新

通过以下方式解决了上述gif问题:

collectionView.isPrefetchingEnabled = false
现在,数据在第一个tableview中正确显示,当我切换到下一个tableview时,新数据将显示在那里,但随后会重复,因此第三个与第一个相同,第四个与第二个tableview相同

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

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委托来实现这一点

类MyVC:UICollectionViewDataSource{
var myData:[数据类型]!//在开始获取委托调用之前,必须使用数据填充此值。您可能应该在初始化此视图后立即设置此值。
func collectionView(collectionView:UICollectionView,cellForRowAt indexPath:indexPath)->UICollectionViewCell{
//配置集合视图单元格
configure(使用:myData[indexPath.row]
返回单元
}
}
类MyCell:UICollectionViewCell,UITableViewDelegate{
@IBVAR表格视图:UITableView!
变量数据:数据类型!
func配置(带数据:数据类型){
self.data=数据
self.tableView.dataSource=self
}
func tableView(tableView:UITableView,cellForRowAt indexath:indexPath)->UITableViewCell{
//使用self.data设置表格单元格
返回单元
}
}

添加一些代码以获得更好的答案。我尝试过,但没有成功,我怀疑我需要将数据源设置为tableview,但我仍然无法修复它。我理解,但我不知道将有多少tableview。因此我无法硬编码25个表委托和数据源。或者您建议将委托存储在多维数组中的某个位置我已经到了可以开始质疑我的代码的时候了,但还没有。谢谢你的回答,欢迎进一步讨论!好的,我会用我认为是解决你问题的好方法更新我的答案。我喜欢这种方法,我今天会测试它!干杯,伙计,这可能会节省我一周的时间。现在我在e中有tableview函数来自ReceivedCell的xtension:UIcollectionviewcell。以及带有UICollectionViewController、UicollectionviewflowdelegateFlowLayout的ReceiveViewController。因此,我无法访问tableview函数中的tableview变量。我仍然无法让它显示其他tableview上的数据。重要的是,我永远不知道tablev的确切数量我需要的视图。它可以是1到25。至少现在是这样。我制作了一个结构,以便能够在这两个类中使用变量。
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