IOS - SWIFT - CollectionView -> ImageView 加载URL图片

3

我有一个带图片的CollectionView应该从网络加载。

URL链接在字符串数组中,而collectionView单元格中是imageView。

我找到了这段代码:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    var cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! ViewCell

    var url = NSURL(string: "\(array[indexPath.row])")

    var err: NSError?
    var imageData: NSData = NSData(contentsOfURL: url!, options: NSDataReadingOptions.DataReadingMappedIfSafe, error: &err)!
    var bgImage = UIImage(data:imageData)

    cell.imgView.image = bgImage

   // cell.imgView.image = UIImage(named: array[indexPath.row])
    cell.load.hidden = false
    cell.load.startAnimating()
    cell.load.hidesWhenStopped = true
    if cell.imgView.image == nil {
        cell.load.startAnimating()
    }else{
        cell.load.stopAnimating()
    }
    return cell
}

问题出现在启动时。 应用程序已加载,我在我的MainVC(带有collectionView)中看到一些println的注释,但在iPad上却冻结了启动屏幕。
我试着将入口点放到另一个视图中,它可以正常工作,直到我segue到MainVC,一切都冻结,没有响应任何触摸。
可能出了什么问题?
谢谢任何帮助!
1个回答

2

你好,我刚刚从developer.apple复制了一个示例,但是遇到了问题。这里有一张code image,看起来我做错了什么,我得到了nil值。顺便说一下,应用程序启动并正常工作,但是只有孤独的加载指示器。 - Ilya Leshkinov

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接