Ios TableViewCell的几个问题

Ios TableViewCell的几个问题,ios,swift,uitableview,swift3,uicollectionviewcell,Ios,Swift,Uitableview,Swift3,Uicollectionviewcell,我当前在加载的单元格中有一个带有uicollectionview的tableview。我目前在tableview单元格方面遇到两个问题 1) tableview中的第一个单元格应具有紫色背景,但在初始加载时显示为白色。我尝试将代码放入初始值设定项中,但是这会使所有单元格加载为单元格0,因此每个单元格都变为紫色;只有当我将它们放入draw()函数时,它们才能正确地绘制,第一个单元格除外 2) 我遇到了一个问题,当单元格离开屏幕时重新加载错误,将发生的是当单元格被放入que时,它会弄乱单元格的内容并

我当前在加载的单元格中有一个带有uicollectionview的tableview。我目前在tableview单元格方面遇到两个问题

1) tableview中的第一个单元格应具有紫色背景,但在初始加载时显示为白色。我尝试将代码放入初始值设定项中,但是这会使所有单元格加载为单元格0,因此每个单元格都变为紫色;只有当我将它们放入draw()函数时,它们才能正确地绘制,第一个单元格除外

2) 我遇到了一个问题,当单元格离开屏幕时重新加载错误,将发生的是当单元格被放入que时,它会弄乱单元格的内容并不断重新绘制它们;我试图清除collectionView数据源并重新加载它,但似乎无法正确执行;我只是不想让单元格在不正确的单元格中被重新绘制;换句话说,在第一个应该是紫色的单元格中,collectionview在离开屏幕后会出现在单元格中。我正在使用PrepareForuse,但清除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")
    }



}

当单元格离开屏幕&我返回它时,应用程序崩溃并说它意外地在以下位置找到了零:
collectionView.dataSource=self
当调用
func-draw(rect:CGRect)
时,您实际上无法控制,因此,您应该在
func-awakeFromNib()中创建集合视图,因为每个单元格只调用一次。背景色你可以像这样直接改变

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)")
    if indexPath.row == 0 {
        cell.backgroundColor = UIColor.purple
    }
    return cell
}

为什么不能在故事板中创建自定义单元并使用它呢。在cellForRowAtIndexPath方法中设置单元格do的颜色,如


如果(indexPath.row==0){cell.backgroundColor=UIColor.purple}

如果我使用awakeFromNib,我将如何在prepareforuse中重置CollectionView单元格,以便它们不会重叠并继续绘制?因此我尝试将代码放在awakeFromNib中,但单元格中没有显示任何内容?我不想听起来很刺耳。。。你在这里有太多的问题要解决。所以-找到并阅读一些关于重复使用简单单元,然后是复杂单元,然后通过代码而不是故事板原型来创建像你这样的单元的教程,等等。然后开始把它们放在一起。在这样做的时候。。。如果您遇到一个教程,告诉您在
中实例化并添加subview(),覆盖func draw(rect:CGRect)
?永远不要回到那个网站。你有我可以使用/研究的资源吗?