【问题标题】:How to Dynamically Add New Cells To UICollectionView?如何向 UICollectionView 动态添加新单元格?
【发布时间】:2019-07-10 15:24:30
【问题描述】:

假设一个集合视图(或表格视图)的部分属性中的项目数已设置为 10(即,在一个部分中,有 10 个单元格)并且两个单元格填充了整个视图。这就是我想要实现的:

当用户向上滑动查看第 9 和第 10 个单元格时,我想通过下载相应的单元格信息并将单元格插入到集合视图中来显示她的新单元格(即第 11 个和第 12 个单元格)。但是直到用户查看最后一个单元格(第 10 个)并向上滑动;不应该下载第 11 和第 12 单元格的数据,并且用户不应该看到单元格(当下载第 11 号和第 12 号单元格时,如果用户再次向上滑动,则将下载第 13 号和第 14 号单元格,依此类推) .

我不知道这个“动作”是否有名称,所以我无法适当地搜索它。有没有简单的实现方法?

【问题讨论】:

  • 您似乎正在寻找一个无限滚动的表格视图。这是谷歌上一个很好的索引主题。这是 Ray Wenderlich 的搜索结果:raywenderlich.com/5786-uitableview-infinite-scrolling-tutorial
  • 你好亚伦!是的,这就是我要找的。非常感谢你让我知道它叫什么;它帮助很大。

标签: ios swift uitableview uicollectionview


【解决方案1】:

如果您按照 Aaron 的建议在 Google 上搜索“无限滚动的表格视图”或“分页”,您会发现很多更好、更复杂的在 iOS 中实现它的教程。但我需要一些简单且“正常工作”的东西,所以这就是我的实现方式:

首先,我定义了五个变量:

let objectsToShowPerUpdate = 5 // I display 5 objects in the collection view, if user scroll down another 5 objects will be download and so on.

var objectIDs = [String]() // These are the IDs for to download objects to be viewed in collection views

var previouslyViewedCellIndexPaths = [Int]() // This will be explained below.
let objectNumberToBeDownloadedTotal = 10 // So I will download 10 objects in this case - I will first download 5 and if user scrolls down will download 5 more.

var objectsArray = [Object]() // will store Object items in this array.

在 viewDidLoad() 中,我将下载并显示前 5 个对象(由 objectsToShowPerUpdate 设置)。

downloadObjectIDsFunction { (downloadedObjectIDs) in
    self.objectIDs = downloadedObjectIDs

    downloadedObjectIDs.forEach({ (objectID) in
        downloadObject(objectID: objectID, { (object) in
            if self.objectsArray.count > self.objectsToShowPerUpdate - 1 { return }
            self.objectsArray.append(object)
            self.yourCollectionView.insertItems(at: [IndexPath(row: self.objectsArray.count - 1, section: 0)])
})
})

设置您的收藏将包含多少项目:

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return objectsArray.count
    }

设置单元格的显示方式:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: yourIdentifier, for: indexPath) as! YourCustomCell
    cell.titleLabel.text = objectsArray[indexPath.row].title
    return cell
}

在这里实现分页:

func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
    if (indexPath.row + 1) % objectsToShowPerUpdate == 0 && indexPath.row + 1 < objectIDs.count && !previouslyViewedCellIndexPaths.contains(indexPath.row + 1) && indexPath.row + 1 < objectNumberToBeDownloadedTotal {

        previouslyViewedCellIndexPaths.append(indexPath.row + 1) // So if user viewed the last cell before, we won't download same objects again.

        let indexes = (indexPath.row + 1)...(indexPath.row + objectsToShowPerUpdate)
        indexes.forEach { (index) in
            downloadObject(objectID: objectIDs[index], { (object) in
                self.objectsArray.append(object)
                self.yourCollectionView.insertItems(at: [IndexPath(row: self.objectsArray.count - 1, section: 0)])
            })
        }
    }
}

如果这对任何人有帮助,我会很高兴!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-19
    • 2016-07-08
    • 2020-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-08
    相关资源
    最近更新 更多