uicollectionView.reloaddata()在添加第一个项目后起作用,然后似乎在后面运行一个



我有一个空字符串作为数据源的空uicollectionView。

当我将第一个字符串添加到数组中并在CollectionView上调用ReloDdata(),这是我所期望的。当我添加第二个字符串并调用reloddata()什么都没发生,但是我的数组肯定会成长。当我添加第三个项目并调用Reloddata()时,第二个项目不会出现,但不出现第三件。每个新添加都会在上一个字符串中显示出来。

如果然后在不添加字符串的情况下调用reloddata(),则最后一个项目出现。

我的代码添加到集合中:

func addKeyword(keyword: String) {
    keywords.append(keyword)
    newKeywordText.text = ""
    keywordCollection.reloadData()
}

和我的CollectionView相关代码:

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    print("keywords: at refresh (keywords.count)")
    return keywords.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let keyword = keywords[indexPath.row]
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "KeywordCloudCell", for: indexPath) as! KeywordCloudCell
    let keywordLabel:UILabel = (cell.viewWithTag(1) as? UILabel)!
    keywordLabel.text = keyword
    return cell
}

我知道NumberOfitemSinsection正在返回正确的计数,因此我不知道为什么它的行为。有什么想法吗?

尝试首先完成您的数组总体,然后完成呼叫 keywordCollection.reloaddata()

您还可以分享如何调用AddKeyword方法

最新更新