我正在 UITableViewCell
中创建自定义 UICollectionView
,没有 Main.Storyboard
。但是 cellForItemAtIndexPath
方法没有被调用,所以你能帮我以最好的方式修复它吗?
这是我的代码:
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/