Ios UICollectionView-插入项(位于:indexPath)不工作

Ios UICollectionView-插入项(位于:indexPath)不工作,ios,swift,uicollectionview,Ios,Swift,Uicollectionview,我有一个数组,其中包含一些元素,我的UICollectionView将显示这些元素。然后我去获取更多元素并将其附加到数组中。然后我想告诉UICollectionView元素已添加到数据源并更新UI 我尝试过这个,但不起作用: // Add more data to my existing array myArray.append(contentsOf: moreElements) let indexPath = [IndexPath(row: myArray.count-1, section:

我有一个数组,其中包含一些元素,我的
UICollectionView
将显示这些元素。然后我去获取更多元素并将其附加到数组中。然后我想告诉
UICollectionView
元素已添加到数据源并更新UI

我尝试过这个,但不起作用:

// Add more data to my existing array
myArray.append(contentsOf: moreElements)
let indexPath = [IndexPath(row: myArray.count-1, section: 0)]
myCollectionView.insertItems(at: indexPath)
我明白了,但我不确定我是否做错了什么


编辑:我不想使用
myCollectionView.reloadData()

您的问题是,对于要添加到集合视图的每个项目,都需要
IndexPath
。将多个对象添加到
myArray
中,然后只将一个
indepath
传递到
insertItems
。这就是错误的原因

请尝试以下操作:

var paths = [IndexPath]()
for item in 0..<moreElements.count {
    let indexPath = IndexPath(row: item + myArray.count, section: 0)
    paths.append(indexPath)
}

myArray.append(contentsOf: moreElements)
myCollectionView.insertItems(at: paths)
var路径=[IndexPath]()

对于0中的项..我收到一个错误,因为我有一个页脚:
无UICollectionViewLayoutAttributes实例-LayoutAttributesForSupplementalElementOfKind:UICollectionElementKindSectionFooter
这是一个全新的问题,超出了您在这个问题中提出的问题。一次只关注一个问题。好的。您的解决方案非常有效(当没有页脚时)。我必须弄清楚我的页脚还发生了什么。