我需要在 Collection View 中的大量 TableView 中填充数据。 tableviews 的数量随着数据的进来而增长,这是未知的。每个 tableview 都在 uicollectionview 单元格中。水平滑动。目前数据显示在第一个表中,第三个,第五个等等。而且所有数据都是一样的。我希望数据只显示在第一位。当我滑动到下一个表格时,该表格会在那里显示新数据。
编辑:
我可以在每隔一个 tableview 中显示数据。在数据仍然存在的 TableView 之间移动,这就是我想要的。
它跳过所有其他 tableview 并显示数据,我不知道为什么!
更新:
indexPath.item 也发生了一些奇怪的事情: 当我在第一个单元格时,它打印 indexPath.item = 0,当我在中间单元格时,它显示它 indexPath.item = 2。
如您所见,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/