2016-05-15 109 views
1

我想在表格視圖單元格內創建集合視圖以便水平滾動元素。但我有問題如何正確填寫我的收藏意見。我需要一種方法將集合視圖與我的數據連接起來。表格視圖單元格內的集合視圖

如何獲取並循環查看錶格視圖單元格以查找所需的集合視圖並返回包含數據的單元格?下面

Collection View in Table View Cell

+0

這應該回購有助於https://github.com/ashfurrow/Collection-View-in-a-Table-View-Cell – kye

+0

良好的形象 - 這是可能的,但複雜。我強烈建議爲每個'UICollectionView'創建一個新的'委託/數據源'模型對象,並將這些對象存儲在一個'Array'中,然後在每個'UITableView'單元格中引用它們。您需要在運行時將每個「UICollectionView」連接到正確的模型對象。這是可行的,但有很多移動部件,所以你的設計需要清楚地思考。 –

+0

@kye謝謝)我已經問過ashfurrow關於我的問題,但它並沒有幫助我:(( – Daryushka

回答

0

是的,你可以怎樣度過的表視圖單元格中的數據收集意見的一般示例。此link是關於此主題的YouTube教程。

型號:

class ListOfParents: NSObject { 
var parents:[Parent]? 
} 

class Parent: NSObject { 
var children: [Child]? 

static func fetchParents(_ completionHandler: @escaping (ListOfParents) ->()) { 
    //fetch parents data 
} 

} 

class Child: NSObject { 

} 

的TableView細胞:

class CustomTableViewController: UITableViewController { 

var listOfParents: ListOfParents? 

override func viewDidLoad() { 
    super.viewDidLoad() 
    Parent.fetchparents { (listOfParents) in 
     self.listOfParents = listOfParents 
    } 
    tableView.register(CustomParentCell.self, forCellReuseIdentifier: "tableCell") 

} 

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    guard let parentsCount = listOfParents?.parents?.count else {return 0} 
    return parentsCount 
} 

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "tableCell", for: indexPath) as! CustomParentCell 

    cell.parent = listOfParents?.parents?[indexPath.item] 
    return cell 
} 
} 

親代細胞:

class CustomParentCell: UITableViewCell, UICollectionViewDelegate, UICollectionViewDataSource { 


var parent: Parent? { 
    didSet { 
     // set child value here 
    } 
} 

lazy var collectionView: UICollectionView = { 
    let layout = UICollectionViewFlowLayout() 
    let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout) 
    return collectionView 
}() 

override init(style: UITableViewCellStyle, reuseIdentifier: String?) { 
    super.init(style: style, reuseIdentifier: reuseIdentifier) 
    collectionView.delegate = self 
    collectionView.dataSource = self 
    collectionView.register(CustomChildCell.self, forCellWithReuseIdentifier: "childCellID") 
} 

required init?(coder aDecoder: NSCoder) { 
    fatalError("init(coder:) has not been implemented") 
} 

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
    guard let childrenCount = parent?.children?.count else {return 0} 
    return childrenCount 
} 

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "childCellID", for: indexPath) as! CustomChildCell 

    cell.child = parent?.children?[indexPath.item] 
    return cell 
} 
} 

子細胞:

class CustomChildCell: UICollectionViewCell { 
var child: Child? 
} 
相關問題