ios - 快速自定义 UITableViewCell 中的 UICollectionView

标签 ios swift uitableview uicollectionview

我正在 UITableViewCell 中创建自定义 UICollectionView,没有 Main.Storyboard。但是 cellForItemAtIndexPath 方法没有被调用,所以你能帮我以最好的方式修复它吗?

enter image description here

这是我的代码:

class IndexRow: UITableViewCell, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {

var names:[String] = ["Movie 1","Movie 2","Movie 3","Movie 4","Movie 5","Movie 6"]



@IBOutlet weak var collectionView: UICollectionView!
override func awakeFromNib() {
    super.awakeFromNib()
    print("Hello")
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return names.count
}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell = self.collectionView.dequeueReusableCellWithReuseIdentifier("videoCell", forIndexPath: indexPath) as UICollectionViewCell
    //let url:NSURL = NSURL(string: names[indexPath.row])!
    //let dt :NSData = NSData(contentsOfURL: url)!
    print("Hello collection")
    let imageView:UIImageView = UIImageView(frame: CGRectMake(5, 5, 180, 270))
    imageView.image = UIImage(named: "poster.jpg")
    imageView.contentMode = .ScaleAspectFit
    cell.addSubview(imageView)

    let lableView:UILabel = UILabel(frame: CGRectMake(5,300,180,35))
    lableView.text = names[indexPath.row]
    lableView.textAlignment = NSTextAlignment.Center
    cell.addSubview(lableView)

    return cell
}

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
    let itemsPerRow:CGFloat = 2
    let hardCodedPadding:CGFloat = 0
    let itemWidth = (collectionView.bounds.width / itemsPerRow) - hardCodedPadding
    let itemHeight = collectionView.bounds.height - (hardCodedPadding)
    return CGSize(width: itemWidth, height: itemHeight)
}

最佳答案

我认为代理未设置,请尝试以下操作:

override func awakeFromNib() {
  super.awakeFromNib()
   print("Hello")
  self.collectionView.delegate = self
  self.collectionView.datasource = self
}

关于ios - 快速自定义 UITableViewCell 中的 UICollectionView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36468801/

相关文章:

ios - Corona "Certificate Not Installed"即使它是

ios - 向船所面对的方向发射子弹 Swift 和 Sprite Kit

ios - 我设置了它,但它不确认 UITableViewDataSource

iOS- swift : Expand user profile picture in UITableView

IOS - 使用自定义 UI TableViewCell 作为 TableFooter

android - Google map 或地点 : API to fetch bulk uploaded (and verified) locations

iphone - 什么更快 : Creating a gradient programmatically using Quartz2D or just importing an image?

ios - AVFoundation - 音乐 iOS

ios - 添加调用另一个类中函数的点击手势

uitableview - 在 View Controller 之间执行 segue 时发生 fatal error