【发布时间】:2017-12-30 09:35:47
【问题描述】:
我目前在加载的单元格中有一个带有 uicollectionview 的 tableview。我目前遇到两个关于 tableview 单元格的问题。
1) tableview 中的第一个单元格应该有紫色背景,但在初始加载时它显示为白色。我尝试将代码放在初始化程序中,但是这会使所有单元格加载为单元格 0,因此每个单元格都变成紫色;只有当我将它们放入 draw() 函数时,它们才能正确绘制,但第一个单元格除外。
2) 我遇到了一个问题,当它们离开屏幕时单元格重新加载不正确,当单元格被放入队列时会发生什么,它会弄乱单元格的内容并不断重绘它们;我尝试清除 collectionView 数据源并重新加载它,但我似乎无法正确执行此操作;我只是不希望单元格在不正确的单元格中不断被重绘;所以换句话说,在第一个应该是紫色的单元格中,一个collectionview会在它离开屏幕后出现在单元格中。我正在使用prepareForReuse,但没有清除collectionview。
这是 cellForItem 的 tableView 代码:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCell(withIdentifier: "MyCell", for: indexPath as IndexPath) as! DiscoverTVC
print("The index path is \(indexPath.row)")
cell.indexPathNum = indexPath.row
return cell
}
这是我的 customCell 本身:
import UIKit
class DiscoverTVC: UITableViewCell, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
var hasLoaded = false
var collectionView : UICollectionView!
var cellId = "Cell"
var index = 0
var indexPathNum = 0
override func prepareForReuse() {
super.prepareForReuse()
collectionView.dataSource = self
collectionView.reloadData()
}
override func draw(_ rect: CGRect) {
print(indexPathNum)
/** Setting up labels in each TableView Cell **/
let cellLabelHeader = UILabel()
/** Collection View within the Tableview **/
let flowLayout : UICollectionViewFlowLayout = UICollectionViewFlowLayout()
flowLayout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
flowLayout.scrollDirection = .horizontal
/** The Indexing of cells within the TableView **/
if indexPathNum == 0 {
self.backgroundColor = UIColor.purple
} else
if ((indexPathNum == 1) || (indexPathNum == 3)) {
/** Setting up the Collection View within the Tableviews **/
self.collectionView = UICollectionView(frame: CGRect(x: self.bounds.width * 0, y: self.bounds.height / 3, width: self.bounds.width, height: self.bounds.height / 1.8), collectionViewLayout: flowLayout)
collectionView.register(DiscoverCVC.self, forCellWithReuseIdentifier: cellId)
collectionView.delegate = self
collectionView.dataSource = self
collectionView.backgroundColor = UIColor.clear
self.addSubview(collectionView)
index = indexPathNum
} else
if ((indexPathNum == 2) || (indexPathNum == 4)) {
/** Setting up the Collection View within the Tableviews **/
self.collectionView = UICollectionView(frame: CGRect(x: self.bounds.width * 0, y: self.bounds.height / 8.5, width: self.bounds.width, height: self.bounds.height / 1.15), collectionViewLayout: flowLayout)
collectionView.register(DiscoverCVC.self, forCellWithReuseIdentifier: cellId)
collectionView.delegate = self
collectionView.dataSource = self
collectionView.backgroundColor = UIColor.clear
self.addSubview(collectionView)
index = indexPathNum
}
}
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
}
/** Collection View Overloads **/
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 10
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! DiscoverCVC
cell.backgroundColor = UIColor.clear
return cell
}
var width : CGFloat = 0
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
if index == 1 || index == 3 {
width = (collectionView.bounds.height)
} else {
width = (collectionView.bounds.height) / 4
}
return CGSize(width:width, height:width)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
fatalError("init(coder:) has not been implemented")
}
}
当单元格离开屏幕并且我返回它时,应用程序崩溃并说它在以下位置意外发现 nil:collectionView.dataSource = self
【问题讨论】:
-
我不想听起来刺耳...你有太多的问题要在这里解决。所以 - 查找并阅读一些关于重用简单单元格的教程,然后是复杂单元格,然后通过代码而不是情节提要原型等创建像您这样的单元格。然后开始将它们放在一起。 这样做的时候...如果你遇到一个教程告诉你实例化和 addSubview() inside
override func draw(_ rect: CGRect)?永远不要回到那个网站。 -
你有我可以使用/研究的资源吗?
标签: ios swift uitableview swift3 uicollectionviewcell