ios - UICollectionView 数据未正确显示

标签 ios swift uicollectionviewcell

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell: MaterialCollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! MaterialCollectionViewCell
    let item = items()[indexPath.item]
    //print(cell.contentView.subviews)
    for view in cell.contentView.subviews{
        view.removeFromSuperview()
    }
    //print(cell.contentView.subviews)
    let colors = [UIColor.grayColor(), UIColor.darkGrayColor()]
    cell.backgroundColor = colors[indexPath.item % 2]

    let showTitle: UILabel = UILabel(frame: CGRect(x: 0, y: 0, width: cell.frame.width, height: cell.frame.height))
    showTitle.text = item.title
    showTitle.textAlignment = .Center
    showTitle.font = UIFont(name: "HelveticaNeue", size: 25.0)
    showTitle.textColor = MaterialColor.white
    showTitle.center = cell.center
    showTitle.hidden = true
    //print(showTitle.text)
    cell.contentView.addSubview(showTitle)


    let imageView: UIImageView = UIImageView(frame: CGRect(x: 0, y: 0, width: cell.frame.width, height: cell.frame.height))
    imageView.kf_setImageWithURL(NSURL(string: item.banner)!,
                                 placeholderImage: nil,
                                 optionsInfo: nil,
                                 progressBlock: { (receivedSize, totalSize) -> () in
                                    //print("Download Progress: \(receivedSize)/\(totalSize)")
        },
                                 completionHandler: { (image, error, cacheType, imageURL) -> () in
                                    //print("Downloaded and set!")
                                    if (image != nil) {
                                        showTitle.removeFromSuperview()
                                    }
                                    else {
                                        showTitle.hidden = false
                                    }
        }
    )
    cell.contentView.addSubview(imageView)
    //print(cell.contentView.subviews)
    return cell
}

这是我用来将数据显示到显示标题标签中的代码。目前 imageView 还没有显示 我的问题是没有显示完整的数据,而是显示了其中的一部分

current output

应该以相同的顺序显示这些

  • 权力的游戏
  • 橙色是新的黑色
  • 马可波罗 (2014)
  • 感兴趣的人
  • 暗物质

最佳答案

我认为问题在于 UILabel View 和 UIImageView 重叠。您应该缩小 UILabel 的大小,并将 UIImageView 放在 UILabel 后面。

关于ios - UICollectionView 数据未正确显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38174676/

相关文章:

ios - 我如何知道什么时候已将某些内容粘贴到 UIWebView 中?

ios - 添加 iAd 横幅时一切都会上升 [SWIFT]

swift - 从 UICollectionViewCell 调用函数以在 UICollectionView 中使用

swift - 返回过渡上的黑边

ios - 与现有 iOS 应用程序集成

ios - 基于 5.0 SDK 构建的 iOS 应用程序可以在 iOS 4.3 上运行吗?

ios - UIApplication SendEvent 崩溃

swift - Xcode - 更改 switch 语句的缩进规则

ios - 在 Swift 中检查字符串是否至少包含大写字母、数字或特殊字符?

ios - 如何从位于 Collection View 单元格内的步进器获取选定的 IndexPath?