Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/kubernetes/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何向UICollectionView动态添加新单元格?_Ios_Swift_Uitableview_Uicollectionview - Fatal编程技术网

Ios 如何向UICollectionView动态添加新单元格?

Ios 如何向UICollectionView动态添加新单元格?,ios,swift,uitableview,uicollectionview,Ios,Swift,Uitableview,Uicollectionview,假设集合视图(或表视图)的分区属性中的项数已设置为10(即,在分区中有10个单元格),两个单元格填充整个视图。这就是我想要实现的: 当用户向上滑动查看第9和第10个单元格时,我想通过下载相应的单元格信息并将单元格插入集合视图来显示她的新单元格(即第11和第12个单元格)。但直到用户查看最后一个单元格(第10个)并向上滑动;第11个和第12个单元格的数据不应该被下载,用户也不应该看到这些单元格(当第11个和第12个单元格被下载时,如果用户再次刷起,这一次将下载第13个和第14个单元格,依此类推)

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

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

我不知道这个“动作”是否有名字,所以我无法正确地搜索它。有没有一种简单的方法来实现它?

如果你在谷歌上搜索Aaron建议的“无限滚动表视图”或“分页”,你会发现很多更好、更复杂的在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设置)

设置集合将包含的项目数:

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)])
            })
        }
    }
}
func collectionView(collectionView:UICollectionView,willDisplay单元格:UICollectionViewCell,forItemAt indexPath:indexPath){
如果(indexPath.row+1)%objectsToShowPerUpdate==0&&indexPath.row+1

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

看起来您正在寻找一个无限滚动的表视图。这是谷歌上一个索引良好的主题。这是Ray Wenderlich的搜索结果:你好Aaron!是的,这就是我要找的。非常感谢你让我知道它叫什么;这帮了大忙。
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)])
            })
        }
    }
}