如何在视图控制器的集合视图中使用多个UICollectionViewCell。

时间:2015-01-30 04:07:15

标签: swift

我有两个单元格类:

class statusCell: UICollectionViewCell {

var textLabel: UILabel!

required init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
}

override init(frame: CGRect) {
    super.init(frame:frame)

    let textFrame = CGRect(x: 0, y: 0, width: 150, height: 200)
    textLabel = UILabel(frame: textFrame)
    textLabel.font = UIFont.systemFontOfSize(UIFont.smallSystemFontSize())
    textLabel.textAlignment = .Center
    contentView.addSubview(textLabel)

}

override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
}
}

class imagePostCell: UICollectionViewCell {

var textLabel: UILabel!
var imageView: UIImageView!

required init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
}

override init(frame: CGRect) {
    super.init(frame:frame)

    imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 159, height: 225))
    imageView.contentMode = UIViewContentMode.ScaleAspectFit
    contentView.addSubview(imageView)

    let textFrame = CGRect(x: 0, y: imageView.frame.size.height, width: 159, height: 125)
    textLabel = UILabel(frame: textFrame)
    textLabel.font = UIFont.systemFontOfSize(UIFont.smallSystemFontSize())
    textLabel.textAlignment = .Center
    contentView.addSubview(textLabel)
}

override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
}

}

这是视图控制器:

let statusCellReuseId = "statusCell"
let imagePostCellReuseId = "imagePostCell"
class ProfileViewController: UIViewController, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {

@IBOutlet var postsView: UICollectionView!


let sectionInsets = UIEdgeInsets(top: 0.0, left: 0.0, bottom: 0.0, right: 0.0)

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

//num of posts per request / page
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 10
}

//creation of posts
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    var cell: statusCell!
    var cell2: imagePostCell!
    if((indexPath.row + 2) % 2 == 0) {
        cell = collectionView.dequeueReusableCellWithReuseIdentifier(statusCellReuseId, forIndexPath: indexPath) as statusCell
        if cell == nil {
            cell = statusCell(frame: CGRectMake(0, 0, collectionView.frame.size.width, collectionView.frame.size.height))
        }
        cell.backgroundColor = UIColor.orangeColor()
        cell.textLabel.text = "Hello mate!"
    }
    else {
        cell2 = collectionView.dequeueReusableCellWithReuseIdentifier(imagePostCellReuseId, forIndexPath: indexPath) as imagePostCell
        if cell2 == nil {
            cell2 = imagePostCell(frame: CGRectMake(0, 0, collectionView.frame.size.width, collectionView.frame.size.height))
        }
        cell2.backgroundColor = UIColor.blueColor()
        cell2.textLabel.text = "Goodbyte!"
        cell2.imageView.image = UIImage(named: "buton")
    }
    return cell
}

//num of sections with posts
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
    //#warning Incomplete method implementation -- Return the number of sections
    return 1
}

//height of cell
func collectionView(collectionView: UICollectionView!,layout collectionViewLayout: UICollectionViewLayout!, sizeForItemAtIndexPath indexPath: NSIndexPath!) -> CGSize {
    return CGSize(width: 159, height: 350)
}

//padding from cellphone
func collectionView(collectionView: UICollectionView!,layout collectionViewLayout: UICollectionViewLayout!,insetForSectionAtIndex section: Int) -> UIEdgeInsets {
    return sectionInsets
}


}

我在viewcontroller中有一个集合视图,collectionview在storyboard中有两个“items”,其中包含id:“statusCell”和“imagePostCell”以尝试连接单元格。两个单元格之间的区别是一个是纯文本,而另一个是带有图像的文本。目前,我只是想让自定义单元格出现并显示示例文本/图像。但是,当我模拟应用程序时,它说“cellForItemAtIndexPath”中的单元格为零。我试图创建一个if语句来尝试解决问题,以及在viewcontroller的viewdidload中以编程方式创建单元格。我的代码有什么问题,单个UICollectionView可以使用的自定义单元格数量是否最大?

1 个答案:

答案 0 :(得分:2)

您可以根据需要使用原型单元格的数量,但您必须在故事板中设置单元格类,每个单元格的出口和可重复使用的标识符。而且你不必检查它是否为零。 此外,如果您不使用故事板,则可以使用 dequeueReusableCellWithReuseIdentifier 按代码对其进行实例化。

请记住,如果您使用的是故事板,那么将执行的方法是 awakeFromNib ,而不是初始化框架。