簡體   English   中英

UITableView內的CollectionView單元重復

[英]CollectionView cell inside UITableView is repeating

我有一個表視圖,其中每個單元格都包含一個集合視圖!

1)在collectionview中僅顯示兩個單元格,因為我的問題是當我們滾動tableview時,collectoinview單元格內容在tableview的許多其他單元格中重復

mycode的:

let tags = Tblarr[indexPath.row]["hashtags"] as! [String]
    if tags.count != 0
    {
        for var i in 0 ... tags.count - 1
        {
            if i < 2
            {
                cell.tagsView.addTag(tags[i])
                cell.collectionView.isHidden =   false
            }
        }
        if tags.count > 2
        {
            cell.lblCount.isHidden = false
            cell.lblCount.text = "+\(tags.count - 2)"
        }
        else
        {
            cell.lblCount.isHidden = true
        }
    }
    else{
        cell.lblCount.isHidden = true
        cell.collectionView.isHidden =   true
    }
    cell.fillCollectionView(with: cell.tagsView.tags)
    cell.widthConstraint.constant = cell.collectionView.contentSize.width

在這里,我只得到固定的collectionview寬度,而沒有根據內容大小設置寬度,並且collectionview單元格在tableview的其他單元格中重復,即使cell為1,它也會顯示2個單元格(滾動時)

第一次加載時

第一次加載時

當tableview滾動時

當tableview滾動時

//用於從tableview將數組存儲在collectionview中

func fillCollectionView(with array: [String]) {
    self.collectionArr = array
    self.collectionView.reloadData()
}

我不知道fillCollectionView是做什么的,但是您似乎不認為表視圖單元格已被重用。 因此,每次調用fillCollectionView ,都必須重置單元格或內容或集合視圖的內容。

在tableview中重新加載collectionView

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { cell.fillCollectionView.reloadData() }

func fillCollectionView(with array: [String]) {
self.collectionArr.removeAll
self.collectionArr = array
self.collectionView.reloadData()

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM