2017-03-17 125 views
0

我的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中着色。

和我運行應用程序後沒有任何事發生,所以有我在這裏犯的任何錯誤?

+0

是否設置UICollectionView代表和datasorse? –

+0

設置collectionview的框架讓collectionView = UICollectionView(frame:.zero,collectionViewLayout:layout) –

回答

1

看起來好像你的setupViews()方法正在被調用。考慮在初始程序中調用它。

+0

非常感謝這個筆記,我也應該註冊單元來回UICollectionView –

+0

是的好趕上!另外作爲一個方面說明:您的'collectionViewCell'類名稱應該大寫。 – PiKey

+0

絕對兄弟。 –

-1

您試圖在您的UICollectionViewCell中繼承UITableViewCell

此:

private class collectionViewCell : UITableViewCell, UICollectionViewDataSource, UICollectionViewDelegate 

應該是:

private class collectionViewCell : UICollectionViewCell, UICollectionViewDataSource, UICollectionViewDelegate 
相關問題