如何在集合视图单元格

时间:2015-10-25 20:24:53

标签: ios swift ios-autolayout

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

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),但仍然是黑屏。

4 个答案:

答案 0 :(得分:3)

你应该使用

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
}

答案 1 :(得分:0)

您需要将图像放在contentView -

cell.contentView.addSubview (...)

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

答案 2 :(得分:0)

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)
}

答案 3 :(得分:0)

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

    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
    }