带有部分的 UItabelview 内的多个 UICollectionviews - swift [英] multiple UICollectionviews inside a UItabelview with sections - swift
问题描述
基本上,我试图获得类似于以下结果的结果:http://ashfurrow.com/blog/putting-a-uicollectionview-in-a-uitableviewcell
Basically, I am trying to obtain something similar to the result of this : http://ashfurrow.com/blog/putting-a-uicollectionview-in-a-uitableviewcell
然而,我想要 2 个 collectionsViews,一个在第一部分,另一个在第二部分
how ever, i'm want 2 collectionsViews, 1 in first section and another in 2nd section
我如何做到这一点?我在这里敲我的头......每次我尝试设置委托和数据源时它都会失败并说我需要注册一个笔尖我也不知道如何在它的 cellForRowAtIndexPath 中分离集合视图,我如何才能看到哪个正在加载?我怎样才能让它正确加载,以便第一部分有 self.genre 而第二部分有 self.radioStat
how do i achieve this ? i'm bashing my head in here... each time i try to set delegate and datasource it fails and says i need to register a nib Also i have no idea how i separate the collection views in it's cellForRowAtIndexPath, how can i see which one is loading ? how can i get it to load properly so that first section has self.genre and 2nd section has the self.radioStat
这是我第二次尝试设置委托和数据源时遇到的错误
this is the error i get when i try to set delegate and datasource for the 2nd time
**由于未捕获的异常NSInternalInconsistencyException"而终止应用程序,原因:无法使类型视图出列:带有标识符 CollectionViewCell34 的 UICollectionElementKindCell - 必须为标识符注册一个 nib 或一个类或连接故事板中的原型单元格"**
**Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'could not dequeue a view of kind: UICollectionElementKindCell with identifier CollectionViewCell34 - must register a nib or a class for the identifier or connect a prototype cell in a storyboard' **
tableViewCellCollectionView.swift
import Foundation
import UIKit
let collectionViewCellIdentifier: NSString = "CollectionViewCell"
class tableViewCellCollectionView: UITableViewCell {
var collectionView: UICollectionView!
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
var layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsetsMake(4, 5, 4, 5)
layout.minimumLineSpacing = 5
layout.itemSize = CGSizeMake(91, 91)
layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
self.collectionView = UICollectionView(frame: CGRectZero, collectionViewLayout: layout)
self.collectionView.registerClass(collectionViewCellTableView.self, forCellWithReuseIdentifier: "CollectionViewCell")
self.collectionView.registerClass(collectionViewCellTableView.self, forCellWithReuseIdentifier: collectionViewCellIdentifier)
self.collectionView.backgroundColor = UIColor.lightGrayColor()
self.collectionView.showsHorizontalScrollIndicator = false
self.contentView.addSubview(self.collectionView)
self.layoutMargins = UIEdgeInsetsMake(10, 0, 10, 0)
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func layoutSubviews() {
super.layoutSubviews()
let frame = self.contentView.bounds
self.collectionView.frame = CGRectMake(0, 0.5, frame.size.width, frame.size.height - 1)
}
func setCollectionViewDataSourceDelegate(dataSourceDelegate delegate: protocol<UICollectionViewDelegate,UICollectionViewDataSource>, index: NSInteger) {
self.collectionView.dataSource = delegate
self.collectionView.delegate = delegate
self.collectionView.tag = index
self.collectionView.reloadData()
}
}
tableViewCellCollectionView34.swift
import Foundation
import UIKit
let collectionViewCellIdentifier34: NSString = "CollectionViewCell34"
class tableViewCellCollectionView2: UITableViewCell {
var collectionView2: UICollectionView!
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
var layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsetsMake(4, 5, 4, 5)
layout.minimumLineSpacing = 1
layout.itemSize = CGSizeMake(86, 54)
layout.scrollDirection = UICollectionViewScrollDirection.Vertical
self.collectionView2 = UICollectionView(frame: CGRectZero, collectionViewLayout: layout)
self.collectionView2.registerClass(collectionViewCellTableView2.self, forCellWithReuseIdentifier: collectionViewCellIdentifier34)
self.collectionView2.backgroundColor = UIColor.lightGrayColor()
self.collectionView2.showsHorizontalScrollIndicator = false
self.contentView.addSubview(self.collectionView2)
self.layoutMargins = UIEdgeInsetsMake(10, 0, 10, 0)
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func layoutSubviews() {
super.layoutSubviews()
let frame = self.contentView.bounds
self.collectionView2.frame = CGRectMake(0, 0.5, frame.size.width, frame.size.height - 1)
}
func setCollectionViewDataSourceDelegate2(dataSourceDelegate delegate: protocol<UICollectionViewDelegate,UICollectionViewDataSource>, index: NSInteger) {
self.collectionView2.dataSource = delegate
self.collectionView2.delegate = delegate
self.collectionView2.tag = index
self.collectionView2.registerClass(collectionViewCellTableView2.self, forCellWithReuseIdentifier: collectionViewCellIdentifier34)
self.collectionView2.reloadData()
}
}
Player.swift
extension Player: UICollectionViewDataSource,UICollectionViewDelegate {
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.radioStat.count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell: collectionViewCellTableView = collectionView.dequeueReusableCellWithReuseIdentifier(reuseCollectionViewCellIdentifier, forIndexPath: indexPath) as collectionViewCellTableView
var rowData: String = self.genre.objectAtIndex(indexPath.row) as String
cell.title.text = rowData as String
let cell2: collectionViewCellTableView2 = collectionView.dequeueReusableCellWithReuseIdentifier(reuseCollectionViewCellIdentifier2, forIndexPath: indexPath) as collectionViewCellTableView2
let userPost: String = self.radioStat.objectAtIndex(indexPath.row) as String
cell2.pinImage.image = UIImage(named: userPost)
let collectionViewArray = self.sourceArray[collectionView.tag] as NSArray
return cell
}
extension Player {
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier(reuseIdentifier, forIndexPath: indexPath) as UITableViewCell
if (indexPath.section == 0){
var cell2: tableViewCellCollectionView = tableView.dequeueReusableCellWithIdentifier(reuseTableViewCellIdentifier, forIndexPath: indexPath) as tableViewCellCollectionView
return cell2
}else if (indexPath.section == 1){
let cell3: tableViewCellCollectionView2 = tableView.dequeueReusableCellWithIdentifier(reuseTableViewCellIdentifier2, forIndexPath: indexPath) as tableViewCellCollectionView2
return cell3
}
return cell
}
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
if (indexPath.section == 0){
if let collectionCell: tableViewCellCollectionView = cell as? tableViewCellCollectionView{
collectionCell.setCollectionViewDataSourceDelegate(dataSourceDelegate: self, index: indexPath.row)
}
}
if (indexPath.section == 1){
if let collectionCell2: tableViewCellCollectionView2 = cell as? tableViewCellCollectionView2{
collectionCell2.setCollectionViewDataSourceDelegate2(dataSourceDelegate: self, index: indexPath.row)
}
}
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if (indexPath.section == 0){
return 50
}
if (indexPath.section == 1){
return 450
}else {
return 100
}
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return sourceArray.count
}
}
推荐答案
您需要知道您的 collectionView
与哪个 (tableView) 部分相关.一种方法是继承 UICollectionView 并向其添加 tvSection
属性 - 就像 Ash Furrow 使用 AFIndexedCollectionView
添加 index代码>属性.
You need to know which (tableView) section your collectionView
relates to. One way to do this would be to subclass UICollectionView and add a tvSection
property to it - in the same way that Ash Furrow uses AFIndexedCollectionView
to add an index
property.
但是您似乎使用 collectionView tag
代替了他的 index
,大概是为了避免子类化.如果是这样,一个类似的躲闪方法是使用 tableViewCell 的 contentView
的 tag
来指示单元格在哪个部分.修改 tableView cellForRowAtIndexPath
方法:
But you seem to be using the collectionView tag
in place of his index
, presumably to avoid subclassing. If so, a similar dodge would be to use the tag
of the tableViewCell's contentView
to indicate which section the cell is in. Amend the tableView cellForRowAtIndexPath
method to do this:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier(reuseIdentifier, forIndexPath: indexPath) as UITableViewCell
if (indexPath.section == 0){
var cell2: tableViewCellCollectionView = tableView.dequeueReusableCellWithIdentifier(reuseTableViewCellIdentifier, forIndexPath: indexPath) as tableViewCellCollectionView
cell2.contentView.tag = indexPath.section
return cell2
} else if (indexPath.section == 1){
let cell3: tableViewCellCollectionView2 = tableView.dequeueReusableCellWithIdentifier(reuseTableViewCellIdentifier2, forIndexPath: indexPath) as tableViewCellCollectionView2
cell3.contentView.tag = indexPath.section
return cell3
}
cell.contentView.tag = indexPath.section
return cell
}
因为collectionView 是作为contentView
的子视图添加的,所以您可以使用superview.tag
确定给定collectionView 的(tableView) 部分.所以修改你的collectionView的cellForItemAtIndexPath
来测试这个:
Because the collectionView is added as a subview of the contentView
, you can determine the (tableView) section for a given collectionView using superview.tag
. So modify your collectionView's cellForItemAtIndexPath
to test this:
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let section = collectionView.superview.tag
if section == 0 {
let cell: collectionViewCellTableView = collectionView.dequeueReusableCellWithReuseIdentifier(reuseCollectionViewCellIdentifier, forIndexPath: indexPath) as collectionViewCellTableView
// Now configure from the data...
var rowData: String = self.genre.objectAtIndex(indexPath.row) as String
cell.title.text = rowData as String
return cell
else {
let cell2: collectionViewCellTableView2 = collectionView.dequeueReusableCellWithReuseIdentifier(reuseCollectionViewCellIdentifier2, forIndexPath: indexPath) as collectionViewCellTableView2
// Now configure from the data...
let userPost: String = self.radioStat.objectAtIndex(indexPath.row) as String
cell2.pinImage.image = UIImage(named: userPost)
return cell2
}
}
这需要一些润色;特别是我不确定您希望如何将数据(sourceArray
、genre
和 radioStat
)映射到 tableView 行和 collectionView 项.(我根据您现有的代码进行了猜测).但这应该可以为您提供一些帮助.
This will need some polishing; in particular I'm not sure how you wish to map your data (sourceArray
, genre
and radioStat
) to the tableView rows and collectionView items. (I've taken a guess based on your existing code). But this should give you something to work with.
这篇关于带有部分的 UItabelview 内的多个 UICollectionviews - swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!