reloadData()不适用于refreshControl

时间:2016-11-03 14:12:57

标签: ios swift uicollectionview reloaddata uirefreshcontrol

我有UICollectionView管理很多单元格。当我删除一个单元格时,我希望单元格与refreshControl一起消失。但我不明白为什么reloadData不起作用。如果有人可以帮我提前谢谢你。

在我看来didLoad:

self.collectionView!.alwaysBounceVertical = true
let refresher = UIRefreshControl()
refresher.tintColor = MyColor.Color
refresher.addTarget(self, action: #selector(PublicListController.refreshStream), forControlEvents: .ValueChanged)
refreshControl = refresher
collectionView!.addSubview(refreshControl!)

collectionView.dataSource = self
self.populateDataBest()

我的功能简单:

func refreshStream() {
    collectionView?.reloadData()
    refreshControl?.endRefreshing()
}

我用populateDataBest方法完成了我的CollectionView:

 func populateDataBest() {
    self.videosService.get(true, completionHandler: {
        videosBest, error in
        dispatch_async(dispatch_get_main_queue(), {
            if error != nil {
                if error!.code == -999 {
                    return
                }
                self.displayError(informations.LocalizedConnectionError)
                return
            }
                self.bestClip = videosBest
                for (indexBest, _) in (self.bestClip?.enumerate())! {
                    let videoBest:Clip = self.bestClip![indexBest]
                    self.pictureArrayVideo.addObject(["clip": videoBest, "group": "BEST"])
                }
                 self.dataSource.updateData(self.pictureArrayVideo)
                 self.collectionView.reloadData()
        })
    })
}

第一次重载工作在我的方法结束时populateDataBest ..

编辑:

我尝试实现删除元素的函数(我在暂时删除方法的参数中只为我的测试添加0)

func refreshStream() {
      dispatch_async(dispatch_get_main_queue(), {
        self.remove(0)
        self.collectionView.reloadData()
    })
     self.refreshControl.endRefreshing()
}

func remove(i: Int) {
    self.listForPager.removeAtIndex(i)
    let indexPath: NSIndexPath = NSIndexPath(forRow: i, inSection: 0)
    self.collectionView.performBatchUpdates({
        self.collectionView.deleteItemsAtIndexPaths(NSArray(object: indexPath) as! [NSIndexPath])
        }, completion: {
            (finished: Bool) in
            self.collectionView.reloadItemsAtIndexPaths(self.collectionView.indexPathsForVisibleItems())
    })
}

我在

之后出现此错误
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of items in section 0.  The number of items contained in an existing section after the update (8) must be equal to the number of items contained in that section before the update (8), plus or minus the number of items inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of items moved into or out of that section (0 moved in, 0 moved out).'

有人知道为什么,可以帮助我PLZ吗?

提前谢谢。

1 个答案:

答案 0 :(得分:1)

如果你从阵列中骑行你的收藏品,你也应该删除该行的项目将消失并最终重新加载。