UItabelview中的多个UICollection视图,包含 - swift [英] multiple UICollectionviews inside a UItabelview with sections - swift

查看:84
本文介绍了UItabelview中的多个UICollection视图,包含 - 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个集合查看,第一部分为1,第二部分为另一部分

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'而终止应用程序,原因:'无法将类视图出列:UICollectionElementKindCell使用标识符CollectionViewCell34 - 必须为标识符注册一个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 添加索引属性。

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 标签代替他的索引,大概是为了避免子类化。如果是这样,类似的躲闪将是使用tableViewCell的 contentView 标签来指示该单元格所在的部分修改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中的多个UICollection视图,包含 - swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆