ios 8 Swift - 带有嵌入式 CollectionView 的 TableView [英] ios 8 Swift - TableView with embedded CollectionView

查看:25
本文介绍了ios 8 Swift - 带有嵌入式 CollectionView 的 TableView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 iOS 编程比较陌生,尝试了一些方法但都无济于事.

I am relatively new to iOS programming and have tried a few things but to no avail.

我想将 CollectionView 放在 TableViewCell 中.我可以单独编码,但不明白如何在 TableViewCell 中设置和引用每个 CollectionView.

I would like to put a CollectionView inside a TableViewCell. I can code each individually, but dont understand how to set and reference each CollectionView within a TableViewCell.

我找到了这个教程,http://ashfurrow.com/blog/putting-a-uicollectionview-in-a-uitableviewcell/ 展示了如何在 Objective-C 中完成,但我一直在与 Obj-C 斗争.

I have found this tutorial, http://ashfurrow.com/blog/putting-a-uicollectionview-in-a-uitableviewcell/ which shows how it can be done in Objective-C but I have always struggled with Obj-C.

有谁知道 Swift 教程或可以提供帮助吗?我正在创建一个简单的项目/代码,我将很快发布以尝试提供帮助.

Does anyone know of a Swift Tutorial or can assist? I am in the process of creating a simple project/code which I will post shortly to try and assist.

编辑 1

我刚刚找到了上述链接的 swift 版本.我现在正在解决这个问题,但似乎过于复杂,修改 AppDelegate.

I have just found the swift version of the above link. I am working through this now, but seems over complicated, modifying the AppDelegate.

https://github.com/DahanHu/DHCollectionTableView

非常感谢罗布

推荐答案

创建一个普通的 UITableView 并在你的 UITableViewCell 中创建 UICollectionView.您的 collectionView 委托和数据源应符合该 UITableViewCell.

Create a usual UITableView and in your UITableViewCell create the UICollectionView. Your collectionView delegate and datasource should conform to that UITableViewCell.

就通过这个

在你的视图控制器中

// Global Variable
var tableView: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()

tableView = UITableView(frame: self.view.bounds)
    tableView.delegate = self
    tableView.dataSource = self
    self.view.addSubview(tableView)

    tableView.registerClass(TableViewCell.self, forCellReuseIdentifier: "TableViewCell")
    tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "NormalCell")
}

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 5
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    if indexPath.row == 3 {
        var cell: TableViewCell = tableView.dequeueReusableCellWithIdentifier("TableViewCell", forIndexPath: indexPath) as! TableViewCell
        cell.backgroundColor = UIColor.groupTableViewBackgroundColor()
        return cell

    } else {
        var cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier("NormalCell", forIndexPath: indexPath) as! UITableViewCell
        cell.textLabel?.text = "cell: (indexPath.row)"

        return cell
    }
}

如您所见,我创建了两个不同的单元格,一个自定义的 TableViewCell,仅在行索引为 3 时返回,其他索引中的基本 UITableViewCell 才返回.

As you can see I've created two different cells, a custom TableViewCell which is returned only when the row index is 3 and a basic UITableViewCell in other indices.

自定义TableViewCell"将拥有我们的 UICollectionView.所以创建一个 UITableViewCell 子类并写下下面的代码.

The custom "TableViewCell" will have our UICollectionView. So Create a UITableViewCell subclass and write down the below code.

import UIKit

class TableViewCell: UITableViewCell, UICollectionViewDataSource, UICollectionViewDelegate {

var collectionView: UICollectionView!

override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)

    let layout = UICollectionViewFlowLayout()
    layout.scrollDirection = UICollectionViewScrollDirection.Horizontal

    collectionView = UICollectionView(frame: self.bounds, collectionViewLayout: layout)
    collectionView.delegate = self
    collectionView.dataSource = self
    collectionView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "CollectionViewCell")
    collectionView.backgroundColor = UIColor.clearColor()

    self.addSubview(collectionView)
}

required init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
}

// MARK: UICollectionViewDataSource
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
    return 1
}


func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 10
}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell: UICollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("CollectionViewCell", forIndexPath: indexPath) as! UICollectionViewCell
    if indexPath.row%2 == 0 {
        cell.backgroundColor = UIColor.redColor()
    } else {
        cell.backgroundColor = UIColor.yellowColor()
    }

    return cell
}
}

希望有帮助.

这篇关于ios 8 Swift - 带有嵌入式 CollectionView 的 TableView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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