Ios 如何在集合视图单元格中插入图像

Ios 如何在集合视图单元格中插入图像,ios,swift,ios-autolayout,Ios,Swift,Ios Autolayout,我想在集合视图单元格中插入三个图像,三个图像。每个单元格一个图像。只有一个部分。 但是当模拟器显示黑屏时,没有任何图像。 以下是我的部分代码: import Foundation import UIKit class CatImagesViewController:UIViewController,UICollectionViewDataSource,UICollectionViewDelegate { func numberOfSectionsInCollectionView(collect

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

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
}
}
<代码>导入基础 导入UIKit 类CatImagesViewController:UIViewController、UICollectionViewDataSource、UICollectionViewDelegate { func numberOfSectionsInCollectionView(collectionView:UICollectionView)->Int{ 返回1 } func collectionView(collectionView:UICollectionView,numberOfItemsInSection:Int)->Int{ 返回3 } func collectionView(collectionView:UICollectionView,cellForItemAtIndexPath indexPath:NSIndexPath)->UICollectionViewCell{ 设con=CatService(测试:“测试”) 设temp=NSUserDefaults() 设number=temp.integerworky(“图像的数量”) 变量title_数组:数组=con.ImageNamesforCategoryAtex(数字) var string:string=title\u数组[indexPath.row] 打印(“indexPath.row\(indexPath.row)”; 打印(“字符串是\(字符串)”) let cell=collectionView.dequeueReusableCellWithReuseIdentifier(“firstCollectionCell”,forIndexPath:indexPath) 让imageview:UIImageView=UIImageView(帧:CGRectMake(50,50,self.view.frame.width-200,50)) 让image:UIImage=UIImage(名称:string)! imageview.image=image //self.view.addSubview(imageview) cell.contentView.addSubview(imageview) 返回单元 } }
我修改了cell.contentView.addSubview(图像视图),但仍然是黑屏。

您需要将图像放在contentView中--

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

您应该使用

cell.contentView.addSubview(imageview)
行用于知道单元格在表中的位置

要设置高度,请执行以下操作:

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
    return CGSizeMake(50, 414)
}
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)
}
您应该使用nsindepath的属性项

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
}
func collectionView(collectionView:UICollectionView,cellForItemAtIndexPath indepath:nsindepath)->UICollectionViewCell{
设con=CatService(测试:“测试”)
设temp=NSUserDefaults()
设number=temp.integerworky(“图像的数量”)
变量title_数组:数组=con.ImageNamesforCategoryAtex(数字)
var string:string=title\u数组[indexPath.item]
打印(“indexPath.row\(indexPath.item)”;
打印(“字符串是\(字符串)”)
let cell=collectionView.dequeueReusableCellWithReuseIdentifier(“firstCollectionCell”,forIndexPath:indexPath)
让imageview:UIImageView=UIImageView(帧:CGRectMake(50,0,self.view.frame.width-200,50))
让image:UIImage=UIImage(名称:string)!
imageview.image=image
cell.contentView.addSubview(imageview)
返回单元
}

Swift 4的代码:
indepath.row
用于知道单元格在表中的位置。要设置高度,请执行以下操作:

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
    return CGSizeMake(50, 414)
}
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)
}

将子视图添加到单元格时,该单元格可以重用,这意味着您将在上下滚动时创建多个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
    }

单元格的高度是多少?我不知道如何设置单元格的高度…我使用let-imageview:UIImageView=UIImageView(frame:CGRectMake(50,50,self.view.frame.width-200,50))let-image:UIImage=UIImage(命名:string)!imageview.image=image cell.contentView.addSubview(图像视图)但仍然不起作用。你是说有趣的collectionView吗?这个类中没有函数tableView。现在,对不起,我要修复。