UITableView中的UICollectionView在滚动时冻结

时间:2018-07-17 05:51:41

标签: ios swift uitableview scroll uicollectionview

我创建了一个类似Gaana Application的演示项目,为此,我在多个UICollectionView中添加了UITableViewCell,它可以正常工作,但是当我滚动UITableView时,UITableView无法滚动

你们能帮我解决这个问题吗?下面是我的代码。

override func viewDidLoad() {
        super.viewDidLoad()

        self.tblVW.tableFooterView = UIView(frame: CGRect.zero)
        self.tblVW.separatorColor = UIColor.clear

        let trendingNib = UINib(nibName: "TrendingCell", bundle: nil)
        self.tblVW.register(trendingNib, forCellReuseIdentifier: "TrendingCell")

        let topChartNib = UINib(nibName: "TopChartCell", bundle: nil)
        self.tblVW.register(topChartNib, forCellReuseIdentifier: "TopChartCell")

        let madeForYouNib = UINib(nibName: "MadeForYouCell", bundle: nil)
        self.tblVW.register(madeForYouNib, forCellReuseIdentifier: "MadeForYouCell")

        let newReleaseNib = UINib(nibName: "NewReleaseCell", bundle: nil)
        self.tblVW.register(newReleaseNib, forCellReuseIdentifier: "NewReleaseCell")

        let featuredArtistNib = UINib(nibName: "FeaturedArtistCell", bundle: nil)
        self.tblVW.register(featuredArtistNib, forCellReuseIdentifier: "FeaturedArtistCell")

        let discoverNib = UINib(nibName: "DiscoverCell", bundle: nil)
        self.tblVW.register(discoverNib, forCellReuseIdentifier: "DiscoverCell")

        let editorsPickNib = UINib(nibName: "EditorsPickCell", bundle: nil)
        self.tblVW.register(editorsPickNib, forCellReuseIdentifier: "EditorsPickCell")

        let gaanaSpecialNib = UINib(nibName: "GaanaSpecialCell", bundle: nil)
        self.tblVW.register(gaanaSpecialNib, forCellReuseIdentifier: "GaanaSpecialCell")
    }

func numberOfSections(in tableView: UITableView) -> Int {
        return 8
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if indexPath.section == 0 {
            let cell:TrendingCell = self.tblVW.dequeueReusableCell(withIdentifier: "TrendingCell", for: indexPath) as! TrendingCell

            cell.selectionStyle = UITableViewCellSelectionStyle.none

            cell.layoutSubviews()
            return cell
        }
        else if indexPath.section == 1 {
            let cell:TopChartCell = self.tblVW.dequeueReusableCell(withIdentifier: "TopChartCell", for: indexPath) as! TopChartCell

            cell.selectionStyle = UITableViewCellSelectionStyle.none

            cell.layoutSubviews()
            return cell
        }
        else if indexPath.section == 2 {
            let cell:MadeForYouCell = self.tblVW.dequeueReusableCell(withIdentifier: "MadeForYouCell", for: indexPath) as! MadeForYouCell

            cell.selectionStyle = UITableViewCellSelectionStyle.none

            cell.layoutSubviews()
            return cell
        }
        else if indexPath.section == 3 {
            let cell:NewReleaseCell = self.tblVW.dequeueReusableCell(withIdentifier: "NewReleaseCell", for: indexPath) as! NewReleaseCell

            cell.selectionStyle = UITableViewCellSelectionStyle.none

            cell.layoutSubviews()
            return cell
        }
        else if indexPath.section == 4 {
            let cell:FeaturedArtistCell = self.tblVW.dequeueReusableCell(withIdentifier: "FeaturedArtistCell", for: indexPath) as! FeaturedArtistCell

            cell.selectionStyle = UITableViewCellSelectionStyle.none

            cell.layoutSubviews()
            return cell
        }
        else if indexPath.section == 5 {
            let cell:DiscoverCell = self.tblVW.dequeueReusableCell(withIdentifier: "DiscoverCell", for: indexPath) as! DiscoverCell

            cell.selectionStyle = UITableViewCellSelectionStyle.none

            cell.layoutSubviews()
            return cell
        }
        else if indexPath.section == 6 {
            let cell:EditorsPickCell = self.tblVW.dequeueReusableCell(withIdentifier: "EditorsPickCell", for: indexPath) as! EditorsPickCell

            cell.selectionStyle = UITableViewCellSelectionStyle.none

            cell.layoutSubviews()
            return cell
        }
        else {
            let cell:GaanaSpecialCell = self.tblVW.dequeueReusableCell(withIdentifier: "GaanaSpecialCell", for: indexPath) as! GaanaSpecialCell

            cell.selectionStyle = UITableViewCellSelectionStyle.none

            cell.layoutSubviews()
            return cell
        }
    }

下面的代码是UITableViewCell代码中的一个。

    class TrendingCell: UITableViewCell {

    @IBOutlet weak var trendingCollectionVW: UICollectionView!
    var arrData = [String]()
    override func awakeFromNib() {
        super.awakeFromNib()

        trendingCollectionVW.dataSource = self
        trendingCollectionVW.delegate   = self

        let trendingSongsNib = UINib(nibName: "TrendingSongCollectionCell", bundle: nil)
        trendingCollectionVW.register(trendingSongsNib, forCellWithReuseIdentifier: "TrendingSongCollectionCell")

    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

//    override func layoutSubviews() {
//        super.layoutSubviews()
//        
//        self.layer.shouldRasterize = true
//        self.layer.rasterizationScale = UIScreen.main.scale
//    }
}

//MARK: - UICollectionView Delegate & DataSource
extension TrendingCell: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 10
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "TrendingSongCollectionCell", for: indexPath) as! TrendingSongCollectionCell

        cell.lblTItle.text = String.init(format: "Indexpath %d", indexPath.item)
        cell.imgVW.image = UIImage.init(named: String.init(format: "trendingSong%d", indexPath.item+1))
        //cell.layoutSubviews()
        return cell
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: (UIScreen.main.bounds.size.width - 15) / 2.9, height: 175)
    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        print("You selected cell #\(indexPath.item)!")
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsetsMake(0, 5, 5, 5)
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 5
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 5
    }
}

当我在UICollectionView内滚动UITableViewCell时,突然增加了15到20 MB的内存。

修改 这是演示项目链接: https://www.dropbox.com/s/k1vpdqn9moli9nw/Gaana.zip?dl=0 任何帮助将不胜感激。 谢谢

2 个答案:

答案 0 :(得分:1)

这是您做错的几件事。

问题:

每次创建笔尖

    let trendingSongsNib = UINib(nibName: "TrendingSongCollectionCell", bundle: nil)

在错误的位置注册笔尖

    cell.trendingCollectionVW.register(trendingSongsNib, forCellWithReuseIdentifier: "TrendingSongCollectionCell")

即使在注册后,您仍尝试使用其他重用标识符出队

    let cell:TrendingCell = self.tblVW.dequeueReusableCell(withIdentifier: "TrendingCell", for: indexPath) as! TrendingCell

即使您成功使单元出队,您也会在每次通话中一次又一次创建笔尖

    let trendingSongsNib = UINib(nibName: "TrendingSongCollectionCell", bundle: nil)

每次创建集合视图时,它都会加载数据

    // whole architecture is responsible for this, no particular code.

解决方案

  1. 您可以注册单元格的@viewDidLoad或在界面生成器中注册

  2. 请勿动态加载笔尖,出队将自动执行。

答案 1 :(得分:0)

您每次都在创建可重复使用的笔尖,并在cellfor方法中调用layoutSubviews()会造成此问题

您可以对代码进行以下更改

let trendingSongsNib = UINib(nibName: "TrendingSongCollectionCell", bundle: nil)
cell.trendingCollectionVW.register(trendingSongsNib, forCellWithReuseIdentifier: "TrendingSongCollectionCell")

将此行放入单元格的awakeFromNib()

因此它将在创建tableviewcell时注册单元格

并停止呼叫cell.layoutSubviews()