ios 8 Swift - 带嵌入式CollectionView的TableView [英] ios 8 Swift - TableView with embedded CollectionView
问题描述
我对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
我刚刚找到了上述链接的快速版本。我现在正在解决这个问题,但似乎过于复杂,修改了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
非常感谢
Rob
Many thanks Rob
推荐答案
创建一个通常的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.
在你的ViewController中只需要经过这个
Just go through this
// 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
}
}
<希望它有所帮助。
Hope it helps.
这篇关于ios 8 Swift - 带嵌入式CollectionView的TableView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!