如何将新单元格插入UICollectionview

时间:2015-04-16 00:31:37

标签: swift uicollectionview

我试图在Facebook应用上重新创建Feed,但我在插入新下载的单元格时遇到问题。正在插入单元格,但是像这样的问题:Trying to insert new items into UICollectionView but getting the same cells over and over,它会继续插入相同的结果,在这种情况下,最初的前10个帖子。

我所做的是:

jsonRequest.getPosts(hashKey!, request: populateFeedRequest) { (succeeded:Bool, msg:String, results:[AnyObject]) -> () in
    var resultsAreDone:Bool!
    if succeeded {
        if (results.isEmpty == false) {
            ++self.lastFeedPostId
            resultsAreDone = true
        } else {
            resultsAreDone = false
        }
    } else {
        println("you weren't able to download the posts")
    }
    dispatch_async(dispatch_get_main_queue(), { () -> Void in
        if (resultsAreDone == true) {
            var updatedArray:Array = [Dictionary<String, AnyObject>]()
            self.postsView.performBatchUpdates({
                let resultsSize = self.feedArray.count
                var size = 0
                if let newArray = results as? [Dictionary<String, AnyObject>] {
                    for n in newArray {
                        self.feedArray.append(n)
                    }
                    size = resultsSize + newArray.count
                }
                var arrayWithIndexPaths = NSMutableArray()
                var i = 0
                for (i = resultsSize; i < size; i++) {
                    arrayWithIndexPaths.addObject(NSIndexPath(forRow: i, inSection: 0))
                }
                self.postsView.insertItemsAtIndexPaths(arrayWithIndexPaths as [AnyObject])
                },
                completion: nil)
        }
        else {
            println("it didn't work")
        }
    })
}

lastFeedPostId用于从服务器获取接下来的10个结果。

也是一个旁注:我在&#34; cellforItemAtIndexPath&#34;中做了这个。功能。我使用当前的Indexpath来确定是否应该从服务器中提取下10个帖子。这是正确的,还是有更好的方法来做到这一点?

非常感谢!!

0 个答案:

没有答案