我的问题是,如何将UICollectionView实现为UITableViewCell (我的UITableViewController),并填充将从UITableViewController加载到UICollectionView中的数据。
我知道如何设置不同的组件,但我不知道如何通过我的UICollectionView的-cellForRow...方法填充数据。
发布于 2018-02-27 16:01:40
你只要经历这一切-
创建一个通常的UITableView,在UITableViewCell中创建UICollectionView。您的collectionView委托和数据源应该符合该UITableViewCell。
在你的ViewController里
// 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。
定制的"TableViewCell“将有我们的UICollectionView。因此,创建一个UITableViewCell子类并写下下面的代码。
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
}
}有关更多信息,您可以看到这个存储库- https://github.com/DahanHu/DHCollectionTableView。
希望这能解决你的问题。谢谢。
发布于 2018-02-27 15:51:52
如果我可以就如何处理这个问题提出建议,我将为存储在表视图控制器上的引用的单元格创建一个数据源对象。请考虑以下几点:
class TableCell: UITableViewCell {
var collectionView: UICollectionView!
}
class TableCellDataSource: NSObject, UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
return UICollectionViewCell()
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 1
}
}
class TableViewController: UITableViewController {
var cellDataSources: [TableCellDataSource] = []
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = TableCell()
let dataSource = TableCellDataSource()
cell.collectionView.dataSource = dataSource
self.cellDataSources.append(dataSource)
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
}请注意,我现在如何控制每个单元格的集合视图从主表视图中显示的内容。
https://stackoverflow.com/questions/49012526
复制相似问题