TableViewCell的夫妻问题

时间:2017-07-24 17:34:21

标签: ios swift uitableview swift3 uicollectionviewcell

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

1)tableview中的第一个单元格应该具有紫色背景,但在初始加载时它看起来是白色的。我尝试将代码放在初始化程序中,但这使得所有单元格都加载为单元格0,因此每个单元格都变为紫色;只有当我把它们放在draw()函数中时才能正确绘制,除了第一个单元格。

2)当我们离开屏幕时,我遇到了细胞重新加载错误的问题,当细胞被放入阙时会发生什么,它会弄乱细胞内容并重新绘制它们;我尝试清除collectionView数据源并重新加载它,但我似乎无法正确地执行它;我只是不希望细胞在不正确的细胞中不断重绘;所以换句话说,在第一个单元格中应该是紫色的,它会在离开屏幕后出现在单元格中。我正在使用prepareForReuse,但是没有运气清理收集视图。

这是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

2 个答案:

答案 0 :(得分:0)

当你调用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
}

答案 1 :(得分:0)

为什么你无法在Storyboard中创建自定义单元格并使用它。并且在cellForRowAtIndexPath方法中为单元格设置颜色,如

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

相关问题