Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 缓存的表viewcell有问题_Swift_Uitableview - Fatal编程技术网

Swift 缓存的表viewcell有问题

Swift 缓存的表viewcell有问题,swift,uitableview,Swift,Uitableview,我有以下代码 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{ let cell = tableView.dequeueReusableCell(withIdentifier: "book", for: indexPath) as! BookTableViewCell cell.cellIndex = indexP

我有以下代码

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
    let cell = tableView.dequeueReusableCell(withIdentifier: "book", for: indexPath) as! BookTableViewCell

    cell.cellIndex = indexPath
    cell.dataSource = self
    cell.delegate = self
    cell.backgroundColor = UIColor.white
    var books = [Book]();

    let count = self.enterpriseBooks.count;
    if count > 0 && indexPath.section < self.enterpriseBooks_2.count {
        books = self.enterpriseBooks_2[indexPath.section]!;
    }

    if (indexPath.section == (count)) {
        books = nytbooks;
    } else if (indexPath.section == (count + 1)) {
        books = trendingbooks;
    } else if (indexPath.section == (count + 2)) {
        books = newbooks
    }

    if (books.count > 0) {
        if (cell.collectionView === nil) {
            cell.addCollectionView();
            cell.collectionView.tag = 124;
            cell.collectionView.reloadData()
        }
    }
    return cell
}
问题是我遇到了缓存问题,其中表视图单元格或集合视图单元格中的内容相互重叠。有人能帮忙吗

上面的内容显示了使用集合视图将图像水平对齐为三行的图像

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionCell", for: indexPath as IndexPath) as! BookCollectionViewCell

    self.dataSource?.getData(cell: self)

    let books = self.books
    let book = books?[indexPath.row]

    if book != nil {
        cell.label.text = book?.title
        cell.imageView.sd_setImage(with: URL(string: book?.image as! String), placeholderImage: nil)
    }

    return cell
}

func addCollectionView () {
    let flowLayout = UICollectionViewFlowLayout()
    flowLayout.scrollDirection = .horizontal

    flowLayout.itemSize = CGSize(width: 100, height: self.frame.height)

    //You can also provide estimated Height and Width
    flowLayout.estimatedItemSize = CGSize(width: 100, height: self.frame.height)

    //For Setting the Spacing between cells
    flowLayout.minimumInteritemSpacing = 25
    flowLayout.minimumLineSpacing = 20

    let cellReuseIdentifier = "collectionCell"
    self.collectionView = UICollectionView(frame: CGRect(x: 10,
                                                         y: 0,
                                                         width: self.frame.width,
                                                         height: self.frame.height),
                                           collectionViewLayout: flowLayout)
    self.collectionView.showsHorizontalScrollIndicator = false
    self.collectionView.translatesAutoresizingMaskIntoConstraints = false
    self.collectionView.backgroundColor = .clear
    self.collectionView.delegate = self
    self.collectionView.dataSource = self
    self.collectionView.register(BookCollectionViewCell.self, forCellWithReuseIdentifier: cellReuseIdentifier)

    self.backgroundColor = UIColor.white
    self.addSubview(collectionView)
}