我有興趣將collectionview作爲集合視圖單元的一部分,但由於某種原因無法弄清楚這將如何完成。我將在哪裏實施細胞collectionview的必要方法?UICollectionViewCell中的UICollectionView
回答
有an article that Ash Furrow wrote,它解釋瞭如何將UICollectionView
放在UITableViewCell
的內部。在UICollectionViewCell
中使用它基本上是一樣的想法。
這對這個答案來說太晚了,但它可能會幫助其他人。這是UICollectionViewCell
內的UICollectionView
的示例。
讓我們開始有一個mainCollectionView
。然後在這個集合中的每個小區創建和初始化一個新的UICollectionView
,合適的地點做的就是在這下面UICollectionView
func collectionView(collectionView: UICollectionView, willDisplayCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath)
例如,我在這裏初始化MainCollectionViewCell
然後MainCollectionViewCell
處理,以創建一個邏輯新UICollectionView
guard let collectionViewCell = cell as? MainCollectionViewCell else { return }
collectionViewCell.delegate = self
let dataProvider = ChildCollectionViewDataSource()
dataProvider.data = data[indexPath.row] as NSArray
let delegate = ChildCollectionViewDelegate()
collectionViewCell.initializeCollectionViewWithDataSource(dataProvider, delegate: delegate, forRow: indexPath.row)
collectionViewCell.collectionViewOffset = storedOffsets[indexPath.row] ?? 0
下面是創建一個新的UICollectionView
MainCollectionViewCell
初始化
func initializeCollectionViewWithDataSource<D: protocol<UICollectionViewDataSource>,E: protocol<UICollectionViewDelegate>>(dataSource: D, delegate :E, forRow row: Int) {
self.collectionViewDataSource = dataSource
self.collectionViewDelegate = delegate
let flowLayout = UICollectionViewFlowLayout()
flowLayout.scrollDirection = .Horizontal
let collectionView = UICollectionView(frame: self.bounds, collectionViewLayout: flowLayout)
collectionView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseChildCollectionViewCellIdentifier)
collectionView.backgroundColor = UIColor.whiteColor()
collectionView.dataSource = self.collectionViewDataSource
collectionView.delegate = self.collectionViewDelegate
collectionView.tag = row
self.addSubview(collectionView)
self.collectionView = collectionView
collectionView.reloadData()
}
希望幫助!!
我爲此做了一個例子,並放在github上。它演示了在UICollectionViewCell
內使用UICollectionView
。
https://github.com/irfanlone/Collection-View-in-a-collection-view-cell
一切程序來完成。沒有故事板。
我在我的UICollectionViewCell中添加了一個UICollectionView。我還展示瞭如何再次添加UICollectionViewCell創建UICollectionView裏面有這樣的結果
import UIKit
class CategoryCell: UICollectionViewCell, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
private let cellId = "cell"
override init(frame: CGRect) {
super.init(frame: frame)
setupViews()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
let appsCollectionView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
return collectionView
}()
func setupViews() {
backgroundColor = .blue
addSubview(appsCollectionView)
appsCollectionView.delegate = self
appsCollectionView.dataSource = self
appsCollectionView.register(AppCell.self, forCellWithReuseIdentifier: cellId)
addConstrainstWithFormat("H:|-8-[v0]-8-|", views: appsCollectionView)
addConstrainstWithFormat("V:|[v0]|", views: appsCollectionView)
}
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)
return cell
}
}
class AppCell: UICollectionViewCell {
override init(frame: CGRect) {
super.init(frame: frame)
setupViews()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func setupViews(){
backgroundColor = .red
}
}
我UICollectionViewController
import UIKit
class FeaturedAppsController: UICollectionViewController, UICollectionViewDelegateFlowLayout {
let cellId = "cell"
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
collectionView?.backgroundColor = .white
collectionView?.register(CategoryCell.self, forCellWithReuseIdentifier: cellId)
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 3
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath)
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(view.frame.width, 150)
}
}
所有的解釋可以發現,被開發的「讓的構建該應用程序「:https://www.youtube.com/watch?v=Ko9oNhlTwH0&list=PL0dzCUj1L5JEXct3-OV6itP7Kz3tRDmma
- 1. 把UICollectionViewCell到UICollectionView
- 2. UICollectionView到UICollectionViewCell
- 3. UICollectionView內UICollectionViewCell
- 4. UICollectionViewCell含有UICollectionView
- 5. 在UICollectionViewCell中實現UICollectionView
- 6. 在UICollectionViewCell中添加UICollectionView
- 7. 在UICollectionView(UIcollectionViewCell)中集成UIWebView
- 8. UICollectionViewCell信號UICollectionView didSelect
- 9. UICollectionView和選定的UICollectionViewCell
- 10. 使UICollectionViewCell出現在UICollectionView的中心
- 11. UICollectionView - 返回for循環中的UICollectionViewCell
- 12. UICollectionView不包含UICollectionViewCell在IB
- 13. UICollectionView和UICollectionViewCell IndexPath錯誤
- 14. 如何從UICollectionViewCell引用UICollectionView?
- 15. - (UICollectionViewCell *)的CollectionView:(UICollectionView *)的CollectionView cellForItemAtIndexPath不叫
- 16. 如何從UICollectionView彈出一個uicollectionViewCell?
- 17. UICollectionView不同segue爲每個UICollectionViewCell
- 18. 平滑滾動在UICollectionView裏面UICollectionViewCell
- 19. UICollectionViewCell大小等於UICollectionView大小
- 20. 如何將UICollectionViewCell鏈接到UICollectionView
- 21. 更UICollectionViewCell添加到現有UICollectionView
- 22. UICollectionView - 要在cellForItemAt中返回的UICollectionViewCell的列表
- 23. 在其awakeFromNib方法中訪問UICollectionViewCell的UICollectionView
- 24. UICollectionView嵌入在UINavigationController中,但UICollectionViewCell框架不正確
- 25. 子視圖中顯示UICollectionViewCell時錯誤滾動UICollectionView
- 26. UICollectionViewCell的實例顯示在UICollectionView的其他實例後面
- 27. 我們如何爲單個UICollectionView使用兩種類型的UICollectionViewCell?
- 28. UICollectionView裏面的UICollectionViewCell - 空區取消觸摸
- 29. Xamarin iOS 8,UICollectionView - 可重複使用的出隊錯誤UICollectionViewCell
- 30. 阻止UICollectionView中每個UICollectionViewCell中的內容隨着滾動而改變?