我目前有一個表格加載單元格內的一個uicollectionview。我目前遇到兩個問題與tableview單元格。幾個問題與TableViewCell
1)tableview中的第一個單元應該具有紫色背景,但在初始加載時會顯示爲白色。我試圖把代碼放入初始化器,但是這會使所有單元格加載爲單元格0,因此每個單元格都變成紫色;只有當我把它們放在draw()函數中時,它們才能正確繪製,除了第一個單元格。
2)我有一個單元格重新加載錯誤,當他們離開屏幕時,會發生什麼時候將單元格放入隊列中,它弄亂單元格的內容是什麼,並不斷重繪它們;我試着清理collectionView數據源並重新加載它,但我似乎無法正確地做到這一點;我只是不想讓細胞繼續在不正確的細胞中重繪;換句話說,在第一個應該是純紫色的單元格中,collectionview在屏幕關閉後會出現在單元格中。我正在使用prepareForReuse,但沒有任何運氣清理collectionview。
這裏是的tableView代碼爲cellForItem:
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")
}
}
當細胞熄滅屏幕&我回到它,應用程序崩潰,並說它意外發現零在:collectionView.dataSource = self
我不想聽起來刺耳......你有太多在這裏解決問題很多。所以 - 找一些關於重新使用簡單單元格,然後複雜單元格的教程,然後通過代碼而不是故事板原型等來創建單元格,然後開始把它們放在一起。 **雖然這麼做...... **如果你遇到一個教程,告訴你在*'override func draw(_ rect:CGRect)'中實例化和addSubview()*?永遠不要回到那個網站。 – DonMag
你有任何我可以使用/研究的資源嗎? – AndrewS