2017-09-10 65 views
0

我使用LBTAComponents莢 這是一個豆莢,使得更容易UICollectionView 工作而無需任何註冊,並提供一個超級簡單的錨固系統......而在第一個項目我有前兩天,我決定使用這個框架 但我現在在uicollectionviewCells一個我需要另一個collectionview,我可以用物品填充,然後我需要它是滾動的問題水平把UICollectionViewCell到UICollectionView

import LBTAComponents 
import UIKit 

class ProductCell: DatasourceCell { 
    let cellId = "cellid" 
    let collectionView : UICollectionView = { 
     let layout = UICollectionViewFlowLayout() 
     let cv = UICollectionView(frame: .zero, collectionViewLayout: layout) 
     cv.backgroundColor = .black 
     return cv 
    }() 

    override func setupViews() { 
    super.setupViews() 

     ProductCell.addSubview(collectionView) 
     collectionView.frame = frame 
     collectionView.register(UICollectionView.self, forCellWithReuseIdentifier: cellId) 
     collectionView.dataSource = self 

    } 

} 

extension ProductCell : UICollectionViewDataSource{ 

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
     return 5 
    } 
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 

     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) 
     cell.backgroundColor = .white 
     return cell 
    } 

} 

datasourceCell等於UICollectionViewCell在這個豆莢裏。

,現在我得到這個錯誤:

instance member 'addsubview' cannot be used on type uiview did you use the value of this type instead? 

你能幫幫我嗎?

我試圖用self.addSubview(collectionView)

,但我得到了另一個錯誤enter image description here

+0

?使用UICollectionView可以輕鬆實現。第三方可能會增加更多的複雜性。 – PGDev

+0

你知道這對我來說是一個挑戰,但我想我會開始傳統的方式... –

+0

傳統方式是什麼? – PGDev

回答

0

您可以簡單地用一個UITableView用含有UICollectionView定製UITableViewCells

實施例:

1.視圖層次

enter image description here

2. UIViewController含有UITableView

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate 
{ 
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int 
    { 
     return 2 
    } 

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 
    { 
     return tableView.dequeueReusableCell(withIdentifier: "tcell", for: indexPath) as! TableCell 
    } 

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat 
    { 
     if indexPath.row == 0 
     { 
      return 120 
     } 
     else 
     { 
      return 150 
     } 
    } 
} 

3.自定義UITableViewCell你爲什麼要使用第三方做的含UICollectionView

class TableCell: UITableViewCell, UICollectionViewDataSource 
{ 
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int 
    { 
     return 3 
    } 

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell 
    { 
     return collectionView.dequeueReusableCell(withReuseIdentifier: "ccell", for: indexPath) 
    } 
} 

4.輸出截圖

enter image description here

0

addsubview是一個實例方法,而不是一個類的方法。所以,你應該使用:而不是

self.addSubview(collectionView) 

ProductCell.addSubview(collectionView) 
+0

句柄我試圖使用這個,但我會得到另一個錯誤,我會把 –

相關問題