简体   繁体   English

UITableView内的CollectionView单元重复

[英]CollectionView cell inside UITableView is repeating

I have one table view in which every cell contains one collection view! 我有一个表视图,其中每个单元格都包含一个集合视图!

1) display only two cell in collectionview as my issue is when we scroll the tableview, collectoinview cell content is repeated in many other cell of tableview 1)在collectionview中仅显示两个单元格,因为我的问题是当我们滚动tableview时,collectoinview单元格内容在tableview的许多其他单元格中重复

myCode: 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

here I get only fixed width of collectionview without setting width according to content size & collectionview cells are repeated in other cell of tableview, even if cell is one , it will show 2 cell(when scrolled) 在这里,我只得到固定的collectionview宽度,而没有根据内容大小设置宽度,并且collectionview单元格在tableview的其他单元格中重复,即使cell为1,它也会显示2个单元格(滚动时)

when load for the first time 第一次加载时

第一次加载时

when tableview is scrolled 当tableview滚动时

当tableview滚动时

//for storing array in collectionview from tableview //用于从tableview将数组存储在collectionview中

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

I don't know what fillCollectionView does but it looks like you didn't consider that the table view cells are reused. 我不知道fillCollectionView是做什么的,但是您似乎不认为表视图单元格已被重用。 So every time you call fillCollectionView you have to reset the content of your cell or the content or your collection view. 因此,每次调用fillCollectionView ,都必须重置单元格或内容或集合视图的内容。

reload collectionView inside tableview 在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