缓存的表格视单元存在问题

时间:2019-12-03 15:44:31

标签: swift uitableview

我有以下代码

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
}

// BookTableViewCell

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)
}

问题是我遇到了缓存问题,其中表视图单元格或集合视图单元格中的内容相互重叠。谁能帮忙吗?

以上内容显示了使用收集视图将图像水平对齐成三行的图像。

0 个答案:

没有答案