草庐IT

ios - 带有部分的 UItabelview 内的多个 UICollectionviews - swift

coder 2023-09-05 原文

基本上,我正在尝试获得类似于此结果的内容:http://ashfurrow.com/blog/putting-a-uicollectionview-in-a-uitableviewcell

无论如何,我想要 2 个 collectionsView,一个在第一部分,另一个在第二部分

我如何实现这一点? 我在这里抨击我的头......每次我尝试设置委托(delegate)和数据源时它都会失败并说我需要注册一个 Nib 我也不知道我如何在它的 cellForRowAtIndexPath 中分离 Collection View ,我怎么才能看到哪个正在加载? 我怎样才能让它正确加载,以便第一部分有 self.genre,第二部分有 self.radioStat

这是我第二次尝试设置委托(delegate)和数据源时遇到的错误

** 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无法使类型的 View 出队:具有标识符 CollectionViewCell34 的 UICollectionElementKindCell - 必须为标识符注册一个 nib 或一个类,或者连接 Storyboard中的原型(prototype)单元格” **

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属性。

但您似乎正在使用 collectionView tag 代替他的 index,大概是为了避免子类化。如果是这样,类似的方法是使用 tableViewCell 的 contentViewtag 来指示单元格所在的部分。修改 tableView cellForRowAtIndexPath方法:

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 的 subview ,您可以使用 superview.tag 确定给定 collectionView 的 (tableView) 部分。所以修改你的 collectionView 的 cellForItemAtIndexPath 来测试这个:

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
    }
}

这需要一些润色;特别是我不确定您希望如何将数据(sourceArraygenreradioStat)映射到 tableView 行和 collectionView 项。 (我根据您现有的代码进行了猜测)。但这应该会给你一些有用的东西。

关于ios - 带有部分的 UItabelview 内的多个 UICollectionviews - swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28125205/

有关ios - 带有部分的 UItabelview 内的多个 UICollectionviews - swift的更多相关文章

  1. ruby-on-rails - Rails 3 中的多个路由文件 - 2

    Rails2.3可以选择随时使用RouteSet#add_configuration_file添加更多路由。是否可以在Rails3项目中做同样的事情? 最佳答案 在config/application.rb中:config.paths.config.routes在Rails3.2(也可能是Rails3.1)中,使用:config.paths["config/routes"] 关于ruby-on-rails-Rails3中的多个路由文件,我们在StackOverflow上找到一个类似的问题

  2. ruby-on-rails - 在 Ruby 中循环遍历多个数组 - 2

    我有多个ActiveRecord子类Item的实例数组,我需要根据最早的事件循环打印。在这种情况下,我需要打印付款和维护日期,如下所示:ItemAmaintenancerequiredin5daysItemBpaymentrequiredin6daysItemApaymentrequiredin7daysItemBmaintenancerequiredin8days我目前有两个查询,用于查找maintenance和payment项目(非排他性查询),并输出如下内容:paymentrequiredin...maintenancerequiredin...有什么方法可以改善上述(丑陋的)代

  3. ruby-on-rails - Rails - 一个 View 中的多个模型 - 2

    我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何

  4. ruby - 多个属性的 update_column 方法 - 2

    我有一个具有一些属性的模型:attr1、attr2和attr3。我需要在不执行回调和验证的情况下更新此属性。我找到了update_column方法,但我想同时更新三个属性。我需要这样的东西:update_columns({attr1:val1,attr2:val2,attr3:val3})代替update_column(attr1,val1)update_column(attr2,val2)update_column(attr3,val3) 最佳答案 您可以使用update_columns(attr1:val1,attr2:val2

  5. ruby-on-rails - 在 ruby​​ .gemspec 文件中,如何指定依赖项的多个版本? - 2

    我正在尝试修改当前依赖于定义为activeresource的gem:s.add_dependency"activeresource","~>3.0"为了让gem与Rails4一起工作,我需要扩展依赖关系以与activeresource的版本3或4一起工作。我不想简单地添加以下内容,因为它可能会在以后引起问题:s.add_dependency"activeresource",">=3.0"有没有办法指定可接受版本的列表?~>3.0还是~>4.0? 最佳答案 根据thedocumentation,如果你想要3到4之间的所有版本,你可以这

  6. ruby - 如何验证 IO.copy_stream 是否成功 - 2

    这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下

  7. Ruby 文件 IO 定界符? - 2

    我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的

  8. ruby - 使用多个数组创建计数 - 2

    我正在尝试按0-9和a-z的顺序创建数字和字母列表。我有一组值value_array=['0','1','2','3','4','5','6','7','8','9','a','b','光盘','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','','u','v','w','x','y','z']和一个组合列表的数组,按顺序,这些数字可以产生x个字符,比方说三个list_array=[]和一个当前字母和数字组合的数组(在将它插入列表数组之前我会把它变成一个字符串,]current_combo['0','0','0']

  9. ruby-on-rails - before_filter 运行多个方法 - 2

    是否有可能:before_filter:authenticate_user!||:authenticate_admin! 最佳答案 before_filter:do_authenticationdefdo_authenticationauthenticate_user!||authenticate_admin!end 关于ruby-on-rails-before_filter运行多个方法,我们在StackOverflow上找到一个类似的问题: https://

  10. ruby-on-rails - Rails 3.1 中具有相同形式的多个模型? - 2

    我正在使用Rails3.1并在一个论坛上工作。我有一个名为Topic的模型,每个模型都有许多Post。当用户创建新主题时,他们也应该创建第一个Post。但是,我不确定如何以相同的形式执行此操作。这是我的代码:classTopic:destroyaccepts_nested_attributes_for:postsvalidates_presence_of:titleendclassPost...但这似乎不起作用。有什么想法吗?谢谢! 最佳答案 @Pablo的回答似乎有你需要的一切。但更具体地说...首先改变你View中的这一行对此#

随机推荐