【问题标题】:UICollectionView not refreshingUICollectionView 不刷新
【发布时间】:2015-03-17 03:31:29
【问题描述】:

我正在下载一些图像并将它们插入到我的 UICollectionView 中

这些是我的班级成员

var myItems:[UIImage] = []
var myView: UICollectionView?

我有一个接收 img:UIImage 对象的定时器函数。我像这样将它插入到我的 UICollectionView 中

        self.myItems.insert(img, atIndex: self.myItems.count)
        var count:Int? = self.myView?.numberOfItemsInSection(0)
        var index:NSIndexPath = NSIndexPath(forRow: count!, inSection: 0)
        self.myView?.insertItemsAtIndexPaths([index])

我也有这个功能:

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return self.myItems.count;
}

还有这个:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as MyCollectionViewCell
    cell.backgroundColor = UIColor.orangeColor()
    cell.textLabel.text = "Text \(indexPath.row)"
    cell.imageView.image = myItems[indexPath.row]
    cell.backgroundView = UIImageView(image: UIImage(named: "cellbg"))
    return cell
}

但是我的视图不会立即显示新图像。我必须点击空白屏幕,然后新单元格及其图像出现。如果有更多单元格适合视图,我也无法滚动视图。

如何刷新视图?我将单元格出列的方式有问题吗?

【问题讨论】:

    标签: swift uicollectionview uicollectionviewcell


    【解决方案1】:

    你忘了重新加载UICollectionView

    self.myView.reloadData()
    

    【讨论】:

      【解决方案2】:

      reloadData 不太好用

      然后我偶然发现了这个SO answer

      解决问题的方法是从主线程调用 reloadData。

      【讨论】:

        猜你喜欢
        • 2013-01-22
        • 1970-01-01
        • 2014-04-10
        • 1970-01-01
        • 2021-12-09
        • 2015-10-19
        • 1970-01-01
        • 1970-01-01
        • 2013-09-08
        相关资源
        最近更新 更多