【问题标题】:how to insert images in collection view cell如何在集合视图单元格中插入图像
【发布时间】:2016-01-24 21:35:37
【问题描述】:

我想在集合视图单元格中插入三张图片,三张图片。每个单元格一张图片。只有一个部分。 但是当模拟器显示黑屏时,没有任何图像。 这是我的代码的一部分:

import Foundation
import UIKit

class CatImagesViewController:UIViewController,UICollectionViewDataSource,UICollectionViewDelegate
{
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
    return 1
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

    return 3
}

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

    let con = CatService(test:"test")

    let temp = NSUserDefaults()
    let  number = temp.integerForKey("num_of_images")

    var title_array:Array<String> = con.imageNamesForCategoryAtIndex(number)

    var string:String = title_array[indexPath.row]

    print("indexPath.row \(indexPath.row)");
    print("string is \(string)")

    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("firstCollectionCell", forIndexPath: indexPath)

    let imageview:UIImageView=UIImageView(frame: CGRectMake(50, 50, self.view.frame.width-200, 50))
    let image:UIImage = UIImage(named:string)!
    imageview.image = image
   // self.view.addSubview(imageview)
    cell.contentView.addSubview(imageview)
    return cell
}
}

我修改了cell.contentView.addSubview(image view),但还是黑屏。

【问题讨论】:

  • 单元格的高度是多少?
  • 不知道如何设置单元格的高度......

标签: ios swift ios-autolayout


【解决方案1】:

你应该使用

cell.contentView.addSubview(imageview)

indexPath.row 用于知道单元格在表格中的位置。

设置高度:

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
    return CGSizeMake(50, 414)
}

你应该使用 NSIndexPath 的属性项

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

let con = CatService(test:"test")

let temp = NSUserDefaults()
let  number = temp.integerForKey("num_of_images")

var title_array:Array<String> = con.imageNamesForCategoryAtIndex(number)

var string:String = title_array[indexPath.item]

print("indexPath.row \(indexPath.item)");
print("string is \(string)")

let cell = collectionView.dequeueReusableCellWithReuseIdentifier("firstCollectionCell", forIndexPath: indexPath)

let imageview:UIImageView=UIImageView(frame: CGRectMake(50, 0, self.view.frame.width-200, 50))
let image:UIImage = UIImage(named:string)!
imageview.image = image
cell.contentView.addSubview(imageview)
return cell
}

【讨论】:

  • 你是说 fun collectionView 吗?该类中没有tableView函数
  • 哦,对不起,我要修复。
【解决方案2】:

您需要将图像放入 contentView --

cell.contentView.addSubview (...)

此外,您在每张图像上重复使用相同的帧,这会导致它们相互重叠。

【讨论】:

  • 我使用 let imageview:UIImageView=UIImageView(frame: CGRectMake(50, 50, self.view.frame.width-200, 50)) let image:UIImage = UIImage(named:string)! imageview.image = image cell.contentView.addSubview(image view) 但还是不行。
【解决方案3】:

Swift 4 的代码:indexPath.row 用于了解表格中的单元格位置。设置高度:

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {   
    return 1   
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)

    var imageview:UIImageView=UIImageView(frame: CGRect(x: 50, y: 50, width: 200, height: 200));

        var img : UIImage = UIImage(named:"Your image name")
        imageview.image = img

        cell.contentView.addSubview(imageview)

    return cell
}

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
    return CGSize(width: 50, height: 414)
}

【讨论】:

    【解决方案4】:

    当您将子视图添加到单元格时,该单元格可以重复使用,这意味着您将在上下滚动时创建多个 UIImageView。这是我要改用的:

        func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath)
            
            for view in cell.contentView.subviews {
                view.removeFromSuperview()
            }
            
            let img = UIImageView(frame: cell.frame)
            img.image = UIImage(named: collectionViewData[indexPath.row])
            cell.contentView.addSubview(img)
            
            return cell
        }
    

    【讨论】:

      【解决方案5】:

      对于 SWIFT 5,您可以使用 switch 语句并创建 CollectionViewCell。

      @IBOutlet weak var imageCell: UIImageView!
      
      override func collectionView(_ collectionView: UICollectionView,
      
      cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
      
      let cell = collectionView.dequeueReusableCell(withReuseIdentifier: 
      
      reuseIdentifier, for: indexPath) as! CollectionViewCell
           
          switch indexPath.row {
      
          case 0:
      
              cell.imageCell.image = ImageLiteral
      
          case 1:
      
              cell.imageCell.image = ImageLiteral
      
          case 2:
      
              cell.imageCell.image = ImageLiteral
      
          default:
      
              break
      
          }
      
          return cell
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-08-20
        • 1970-01-01
        • 1970-01-01
        • 2016-05-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多