集合视图单元格未正确加载图像

Dan*_*ram 10 ios uicollectionview uicollectionviewcell swift alamofire

我正在开发一个iOS应用程序,它将服务器中的帖子加载到UICollectionView中.集合视图单元格包含一个固定在单元格底部的UIImageView.

UICollectionViewCell

每当我启动应用程序并加载集合视图时,所有图像都无法正确加载,而是最后一个图像,这是正确的尺寸.所有单元格的格式都相同.

UICollectionView

我尝试了很多解决方案,但到目前为止还没有任何效果.

我怀疑发生的是图像在设置到每个单元格的UIImageView之前没有完成加载(在这种情况下除了最后一个).虽然在获得成功响应后重新加载单元格,但这似乎不可能.

这是我特定功能的代码(使用Alamofire)

func getAllPosts(){

    let url =  "\(Constants.baseURL)/posts/"
    let parameters = ["user_id": "\(userProfile!.getId())"]

    Alamofire.request(.POST, url, parameters: parameters, encoding: .JSON)
        .validate(contentType: ["application/json"])
        .responseString { response in
            switch response.result {

            case .Success:

                var postsArray = Array<[String: AnyObject]>()

                do {

                    let json = try NSJSONSerialization.JSONObjectWithData(response.data!, options: NSJSONReadingOptions.MutableContainers)



                    for post in json as! [AnyObject] {

                        postsArray.append(post as! [String: AnyObject])

                    }

                    //invert array of posts so that latest load first!

                    postsArray = postsArray.reverse()

                } catch {

                }

                //convert json to Post objects and reload the view

                self.initialisePosts(postsArray)

                self.collectionView?.reloadData()


            case .Failure(let error):
                print(error)
            }
    }  
}
Run Code Online (Sandbox Code Playgroud)

所有帮助表示赞赏.

编辑:下面是UIImageView的当前约束

约束

编辑2:这是格式化单元格的代码

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

    // get a reference to our postcell with no image
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifiers[0], forIndexPath: indexPath) as! PostCell

    guard let _: Post? = posts[indexPath.row] else {
        return cell
    }

    return configurePostCell(cell, post: posts[indexPath.row])!
}

func configurePostCell(cell: UICollectionViewCell, post: Post) -> PostCell?{
    if let cell = cell as? PostCell {

        cell.author.text = post.user_name
        cell.date.text = formatter.stringFromDate(post.pub_date!)
        cell.desc.text = post.desc


        cell.layer.cornerRadius = 5 //set corner radius here

        cell.advertImage.image = post.advert?.advert_image


        return cell
    }

    return nil
}
Run Code Online (Sandbox Code Playgroud)

更新:使用@迈克尔的意见,我发现,细胞图像正确加载..

正确加载图像

但是细胞高度被50px神秘地裁剪掉了..

故事板编辑器单元大小

故事板编辑器单元大小

运行时的单元大小

运行时的单元大小

这似乎是问题,但我还没有找到解决方案.

更新:在奖金还剩两个小时后,我决定将其奖励给@Michael,因为他的回答帮助我进一步调查我的问题,这个问题仍然存在.

Mic*_*ael 3

首先,使用“调试视图层次结构”按钮在运行时检查表单元格中是否存在异常。

然后在单元格创建上断点并检查您要设置到单元格的 UIImage 并确保大小正确。如果将下载的图像分配给本地变量,您也可以快速查看它,看看它看起来是否正确。

然后,如果所有这些看起来都合理,请检查您的自动布局代码是否可靠。

通过从情况中删除图像下载来进行检查。我会这样做:

  1. 完全删除图像的设置,但将图像背景设置为 UIColor.Red 或其他内容,并检查它们的布局是否比正常的 UIView 好一点。
  2. 将图像作为 PNG 嵌入到应用程序包中,并设置它而不是下载的图像。再次,看看它是如何渲染的。
  3. 在应用程序包中尝试不同尺寸的图像,然后再次检查(如果相关 - 也许所有图像的尺寸都相同)

如果一切看起来都不错,那么您就知道问题出在这些服务器映像的下载或布局上。