ios - TableViewCell 的几个问题

标签 ios swift uitableview swift3 uicollectionviewcell

我目前有一个表格 View ,其中加载的单元格中有一个 uicollectionview。我目前遇到了表格 View 单元格的两个问题。

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

2)我遇到了单元格离开屏幕时重新加载不正确的问题,当单元格放入队列时,会发生什么情况,它会弄乱单元格的内容并不断重绘它们;我尝试清除 collectionView 数据源并重新加载它,但我似乎无法正确执行此操作;我只是不希望单元格继续在错误的单元格中重绘;换句话说,在第一个应该是紫色的单元格中, Collection View 在离开屏幕后将出现在该单元格中。我正在使用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
}

这是我的自定义单元本身:

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() 中创建 Collection View ,因为每个单元格仅调用一次。您可以像这样直接更改背景颜色

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
}

关于ios - TableViewCell 的几个问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45286694/

相关文章:

sql - 在 Swift 中通过 SQLClient 从 SQL Server 提取数据

ios - 从 Nib 加载时 UIView 的帧大小?

swift - 为什么 bool 占用 7 个字节的内存?

ios - 长按时 MKPointAnnotation 变为红色图钉

ios - 通过表格附件按钮将字符串值传递给不同的 View Controller

ios - 如何在 IOS Delphi 上的 TWebbrowser 中设置用户代理

iphone - 自定义函数 "indexpath"

ios - OLA 和 Uber 在开车时使用哪种方法获取汽车位置?

ios - 从 UIViewController 到 UITableViewController

iphone - UIView 在 UITableViewController 的中心