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

ios - 收集 View 单元无法正确加载图像-LMLPHP

每当我启动应用程序并加载集合 View 时,所有图像都不会正确加载,而是最后一张图像(正确的尺寸)无法正确加载。所有单元格都以相同的方式格式化。

ios - 收集 View 单元无法正确加载图像-LMLPHP

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

我怀疑正在发生的事情是,在将图像设置为每个单元的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)
            }
    }
}

感谢所有帮助。

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

ios - 收集 View 单元无法正确加载图像-LMLPHP

编辑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
}

更新:使用@Michael的建议,我发现单元格图像正在正确加载。

ios - 收集 View 单元无法正确加载图像-LMLPHP

,但是该单元格的高度被神秘地裁剪了50px。.

Storyboard编辑器单元格大小

ios - 收集 View 单元无法正确加载图像-LMLPHP

运行时的像元大小

ios - 收集 View 单元无法正确加载图像-LMLPHP

这似乎是问题所在,但我尚未找到解决方案。

更新:悬赏期还剩下两个小时,我决定将其授予@Michael,因为他的回答帮助我进一步调查了我的问题,该问题仍在继续。

最佳答案

首先,使用“调试 View 层次结构”按钮在运行时检查表单元格中的异常情况。

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

然后,如果这一切看起来合情合理,请检查您的自动布局代码是否健壮。

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

  • 完全删除图像的设置,但是将图像背景设置为UIColor.Red或其他内容,并检查它们的布局是否与普通UIViews差不多。
  • 将图像作为PNG嵌入应用程序 bundle 包中,并设置它而不是下载的图像。再次,看看它是如何渲染的。
  • 在应用程序 bundle 包中尝试使用其他尺寸的图像,然后再次检查(如果相关-也许所有图像的尺寸都相同)

  • 如果一切看起来不错,那么您就知道这些服务器镜像的下载或布局。

    07-28 01:52
    查看更多