嘿,我想弄清楚。在我将一个字符串附加到作为 Collection View 数据引用的数组之后。我如何才/只重新加载最新的字符串/索引最近附加数组而不是整个 Collection View
好的,所以我知道到目前为止这会重新加载所有内容,这将循环遍历数据源/字符串数组中的所有索引:
log.reloadData()
所以我追加字符串:
// collectionViewDataSource is a UICollectionView
var collectionViewDataSource = [String]()
collectionViewDataSource.append("new string")
重新加载:
// now to figure out how to update the view for just that index and not complete reload every cell, but just add a new cell
log.reloadItems(at: <#T##[IndexPath]#>)
最佳答案
需要确定新增数据的索引路径。在你的情况下,这是微不足道的。在添加新对象之前使用数组的计数。
let newIndexPath = IndexPath(item: collectionViewDataSource.count, section: 0)
collectionViewDataSource.append("new string")
collectionView.insertItems(at: [newIndexPath])
请注意,您需要向 Collection View 中插入一个新项目,而不是重新加载一个项目。
关于ios - 如何将数据添加到 CollectionView 中,仅用于数组 Swift 中的新附加索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49288957/