如何deinit CollectionView类

时间:2017-10-23 08:23:28

标签: swift memory-leaks retain-cycle

在我的项目中有2个ViewControllers(ViewController和DetailViewController)。第一个是UIScrollview中的表格视图。在UICollectionView的第二个按钮上有图像和链接。当我多次在DetailViewController中弹出时,应用程序启动速度变慢,我在图表上看到了泄漏内存。同样在Debug内存中我看到了CollectionViewCell的18个对象和3个DetailViewController,CellProps的第一次泄漏我解决了:

   override func viewDidDisappear(_ animated: Bool) {
    btnArray.removeAll()

如何解决此内存泄漏?

ViewController.swift

import UIKit
import BetterSegmentedControl
import Firebase

class ViewController: UIViewController, UIScrollViewDelegate, UITableViewDelegate {

    var ICOListGoingOn = [ICOs] ()
    var ICOListEnded = [ICOs] ()
    var ICOListnotstarted = [ICOs] ()
    @objc func reload(n: NSNotification) {
        SaveLikedArray()
        self.icoArraySort()
        let slide = SlideView(frame: CGRect(x: view.frame.width * CGFloat(0), y: 0, width: view.frame.width, height: slideScrollView.frame.height))
        slide.ICO = ICOListLiked
        slide.delegate = self
        slideScrollView.addSubview(slide)
    }


extension ViewController: SlideViewDelegate{
    func tableCellSelected(tableView: UITableView, indexPath: IndexPath, ico: ICOs) {
//        print("Table tag : \(tableView.tag) Selcted Row : \(indexPath.row) Selected Value : \(ico)")
        if let controller = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "DetailViewController") as? DetailViewController{
            controller.ICO = ico
            ICOId = ico.ICOId
            self.navigationController?.pushViewController(controller, animated: true)
        }
    }
}

CollectionViewCell.swift

import UIKit

class CellProps {
    var image: UIImage!
    var url: String!
    init (image: UIImage, url: String) {
        self.image = image
        self.url = url
    }
    deinit {
        print("CellProps")
    }
}
protocol CollectionViewCellDelegate {
    func didButtonClick(url: String)
} 
class CollectionViewCell: UICollectionViewCell {
    var delegate: CollectionViewCellDelegate!
    weak var CellItem: CellProps!
    @IBOutlet weak var btnICONOutlet: UIButton!
    @IBAction func btnICONAction(_ sender: Any) {
        delegate?.didButtonClick(url: CellItem.url)
    }
    func setCell(cell: CellProps) {
        CellItem = cell
        CellItem.url = cell.url
        btnICONOutlet.setImage(cell.image, for: .normal)
    }
    deinit {
        print("CollectionViewCell")
    }
}

DetailViewController.swift

import UIKit
class DetailViewController: UIViewController  {
    var ICO: ICOs!
    var btnArray: [CellProps] = []
    var timer: Timer?

    @IBOutlet weak var outCollectioView: UICollectionView!
    @IBOutlet weak var btnLike: LikeButton!
    @IBAction func btnLikeAction(_ sender: Any) {
        if !btnLike.isOn {
            let ind = CountLikedArray(id: ICO.ICOId)
            if ind != -1 {
                ICOListLiked.remove(at: ind)
                lblLike.text = "Вы еще не лайкнули проект"
            }
        }
        if btnLike.isOn {
            let ind = CountLikedArray(id: ICO.ICOId)
            if ind == -1{
                ICOListLiked.append(self.ICO)
                lblLike.text = "Вам нравится проект"
            }
        }
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: "del"), object: nil)
    }
    func FillButtonArray () {
        for value in ICO.news.values {
            var btn : CellProps!
            if value.lowercased().range(of:"t.me") != nil {
                btn = CellProps(image: #imageLiteral(resourceName: "telegram"), url: value)
            }
            else if value.lowercased().range(of:"bitcointalk.org") != nil {
                btn = CellProps(image: #imageLiteral(resourceName: "bitcoin"), url: value)
            } else {
                btn = CellProps(image: #imageLiteral(resourceName: "link"), url: value)
            }
            btnArray.append(btn)
        }
    }
    deinit {
        print("deinit detail")
    }

    override func viewDidDisappear(_ animated: Bool) {
        btnArray.removeAll()
        timer = nil
        outCollectioView = nil
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}
extension DetailViewController: UICollectionViewDelegate, UICollectionViewDataSource, CollectionViewCellDelegate {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return btnArray.count
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let ICONs = btnArray[indexPath.row]
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CollectionViewCell
        cell.setCell(cell: ICONs)
        cell.delegate = self
        return cell
    }
    func didButtonClick(url: String) {
        let ICONURL = URL (string: url)!
         UIApplication.shared.open(ICONURL as URL)
    }
}

if i debug memory graph there are 3 DatailViewController, and 18 CollectionViewCell

1 个答案:

答案 0 :(得分:1)

我找到了解决问题的自我的懒惰方式,它是

    outCollectioView.removeFromSuperview()
    btnLike.removeFromSuperview()

但是DetailViewController仍在增加((