我的UITableView從故事板創建這個我如何使用cellForRowAt indexPath FUNCUICollectionView以編程方式添加到UITableViewCell裏面,但沒有出現?
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row == 0 {
let titleCell = tableView.dequeueReusableCell(withIdentifier: "titleCell", for: indexPath) as! MenuTableViewCell
titleCell.label.text = newsTitle
return titleCell
}else if indexPath.row == 1 {
let collectionViewCell = tableView.dequeueReusableCell(withIdentifier: "collectionViewCell", for: indexPath) as! collectionViewCell
return collectionViewCell
}
let cell = UITableViewCell()
return cell
}
我創造了這個類UICollectionView編程:
private class collectionViewCell : UITableViewCell, UICollectionViewDataSource, UICollectionViewDelegate {
private let reuseableCell = "CellId"
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseableCell)
}
override func layoutSubviews() {
super.layoutSubviews()
}
let imagesCollectionView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = UICollectionViewScrollDirection.horizontal
let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
collectionView.backgroundColor = UIColor.white
collectionView.translatesAutoresizingMaskIntoConstraints = false
collectionView.showsHorizontalScrollIndicator = false
collectionView.showsVerticalScrollIndicator = false
return collectionView
}()
func setupViews() {
backgroundColor = UIColor.black
addSubview(imagesCollectionView)
imagesCollectionView.dataSource = self
imagesCollectionView.delegate = self
addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[v0]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": imagesCollectionView]))
addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[v0]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": imagesCollectionView]))
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 5
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = UICollectionViewCell()
cell.backgroundColor = .green
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: 100, height: frame.height - 68)
}
}
,你看,我已經改變了顏色UICollectionView和它的單元格,但沒有在UITableView中着色。
和我運行應用程序後沒有任何事發生,所以有我在這裏犯的任何錯誤?
是否設置UICollectionView代表和datasorse? –
設置collectionview的框架讓collectionView = UICollectionView(frame:.zero,collectionViewLayout:layout) –