From 780481c07ee0d8f0716d8194c099819906d49bb4 Mon Sep 17 00:00:00 2001 From: stan <3421902@qq.com> Date: Fri, 22 Sep 2017 09:23:18 +0800 Subject: [PATCH] fix memery bug --- GrandMenu/GrandBaseTable.swift | 1 - GrandMenu/GrandCellTableViewCell.swift | 12 ---------- GrandMenu/GrandContentViewController.swift | 3 +-- GrandMenu/GrandMenuTable.swift | 24 +------------------ GrandMenu/GrandPageContentView.swift | 9 ++----- .../ViewControllerDemoViewController.swift | 9 ++++--- .../ViewDemoViewController.swift | 2 +- 7 files changed, 9 insertions(+), 51 deletions(-) diff --git a/GrandMenu/GrandBaseTable.swift b/GrandMenu/GrandBaseTable.swift index 3750296..d63e636 100644 --- a/GrandMenu/GrandBaseTable.swift +++ b/GrandMenu/GrandBaseTable.swift @@ -13,5 +13,4 @@ class GrandBaseTable: UITableView,UIGestureRecognizerDelegate { func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool { return true } - } diff --git a/GrandMenu/GrandCellTableViewCell.swift b/GrandMenu/GrandCellTableViewCell.swift index f0bf66b..6465581 100644 --- a/GrandMenu/GrandCellTableViewCell.swift +++ b/GrandMenu/GrandCellTableViewCell.swift @@ -22,16 +22,4 @@ class GrandCellTableViewCell: UITableViewCell { } } } - - override func awakeFromNib() { - super.awakeFromNib() - // Initialization code - } - - override func setSelected(_ selected: Bool, animated: Bool) { - super.setSelected(selected, animated: animated) - - // Configure the view for the selected state - } - } diff --git a/GrandMenu/GrandContentViewController.swift b/GrandMenu/GrandContentViewController.swift index a50ccc8..5ee507b 100644 --- a/GrandMenu/GrandContentViewController.swift +++ b/GrandMenu/GrandContentViewController.swift @@ -18,8 +18,7 @@ class GrandContentViewController: UIViewController,UITableViewDataSource,UITable super.viewDidLoad() tableView = UITableView() tableView.dataSource = self - tableView.delegate = self - + tableView.delegate = self view.addSubview(tableView) } override func viewWillLayoutSubviews() { diff --git a/GrandMenu/GrandMenuTable.swift b/GrandMenu/GrandMenuTable.swift index ef39260..8fad108 100644 --- a/GrandMenu/GrandMenuTable.swift +++ b/GrandMenu/GrandMenuTable.swift @@ -27,7 +27,7 @@ open class GrandMenuTable: UIView { } } - fileprivate var parentViewController:UIViewController? + weak fileprivate var parentViewController:UIViewController? fileprivate var childViewController:[UIViewController]? fileprivate var collectionView:UICollectionView? fileprivate var startOffsetX:CGFloat = 0.0 @@ -92,28 +92,6 @@ extension GrandMenuTable:UICollectionViewDataSource,UICollectionViewDelegate{ if isSelectBtn { return } -// let scrollWidth = scrollView.bounds.size.width -// let currentOffsetX = scrollView.contentOffset.x -// let startIndex = floor(startOffsetX / scrollWidth) -// var endIndex:CGFloat = 0 -// var progress:CGFloat = 0 -// if currentOffsetX > startOffsetX { -// progress = (currentOffsetX - startOffsetX)/scrollWidth -// endIndex = startOffsetX + 1 -// if endIndex > CGFloat(self.childViewController!.count - 1) { -// endIndex = CGFloat(self.childViewController!.count - 1) -// } -// } -// else if currentOffsetX == startOffsetX{ -// progress = 0.0 -// endIndex = startIndex -// } -// else{ -// progress = (startOffsetX - currentOffsetX)/scrollWidth -// endIndex = startIndex - 1 -// endIndex = endIndex < 0 ? 0 : endIndex -// } - } public func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) { diff --git a/GrandMenu/GrandPageContentView.swift b/GrandMenu/GrandPageContentView.swift index b874fd9..6f67ebf 100644 --- a/GrandMenu/GrandPageContentView.swift +++ b/GrandMenu/GrandPageContentView.swift @@ -17,7 +17,7 @@ import UIKit class GrandPageContentView: UIView { let cellId = "GrandCelId" - open var delegate:GrandPageContentViewDelagate? + weak open var delegate:GrandPageContentViewDelagate? open var contentViewCurrentIndex = 0{ didSet{ if contentViewCurrentIndex < 0 || contentViewCurrentIndex > self.childViewController!.count-1{ @@ -34,7 +34,7 @@ class GrandPageContentView: UIView { } } - fileprivate var parentViewController:UIViewController? + weak fileprivate var parentViewController:UIViewController? fileprivate var childViewController:[UIViewController]? fileprivate var collectionView:UICollectionView? fileprivate var startOffsetX:CGFloat = 0.0 @@ -64,7 +64,6 @@ class GrandPageContentView: UIView { } addSubview(self.collectionView!) - } @@ -121,7 +120,6 @@ extension GrandPageContentView:UICollectionViewDataSource,UICollectionViewDelega endIndex = startIndex - 1 endIndex = endIndex < 0 ? 0 : endIndex } - delegate?.contentViewDidScroll?(contentview: self, startIndex: startIndex, endIndex: endIndex, progress: progress) } @@ -133,7 +131,4 @@ extension GrandPageContentView:UICollectionViewDataSource,UICollectionViewDelega delegate?.contentViewDidEndDecelerating?(contentview: self, startIndex: startIndex, endIndex: endIndex) } - - - } diff --git a/GrandMenuDemo/GrandMenuDemo/ViewControllerDemoViewController.swift b/GrandMenuDemo/GrandMenuDemo/ViewControllerDemoViewController.swift index d68d4b8..17e4320 100644 --- a/GrandMenuDemo/GrandMenuDemo/ViewControllerDemoViewController.swift +++ b/GrandMenuDemo/GrandMenuDemo/ViewControllerDemoViewController.swift @@ -23,7 +23,9 @@ class ViewControllerDemoViewController: UIViewController { navigationItem.rightBarButtonItems?.append(btn2) grandMenu = GrandMenu(frame:CGRect(x: 0, y: 64, width: UIScreen.main.bounds.size.width, height: 40) , titles: ["First","Second","Third","FouthFouthFouth","Fifth","1","2","Thir333333d","Fo44uth","7"]) grandMenu?.backgroundColor = UIColor.white - grandMenu?.selectMenu = scrollCallback + grandMenu?.selectMenu = {[weak self](item:GrandMenuItem, index:Int) in + self?.grandMenuTable?.contentViewCurrentIndex = index + } grandMenu?.itemColor = UIColor.green grandMenu?.itemFont = 14 grandMenu?.itemSelectedFont = 18 @@ -70,10 +72,7 @@ class ViewControllerDemoViewController: UIViewController { } - - func scrollCallback(_ item:GrandMenuItem, index:Int){ - grandMenuTable?.contentViewCurrentIndex = index - } + func changeColor(){ let index = Int(arc4random()) % colors.count grandMenu?.itemSeletedColor = colors[index] diff --git a/GrandMenuDemo/GrandMenuDemo/ViewDemoViewController.swift b/GrandMenuDemo/GrandMenuDemo/ViewDemoViewController.swift index 41ae009..44ffa7a 100644 --- a/GrandMenuDemo/GrandMenuDemo/ViewDemoViewController.swift +++ b/GrandMenuDemo/GrandMenuDemo/ViewDemoViewController.swift @@ -114,7 +114,6 @@ class ViewDemoViewController: UIViewController,UITableViewDelegate,UITableViewDa } func scrollViewDidScroll(_ scrollView: UIScrollView) { - print("big scroll + \(scrollView.contentOffset.y)") let bottomCellOffset = tb.rect(forSection: 1).origin.y - 64 if scrollView.contentOffset.y >= bottomCellOffset { scrollView.contentOffset = CGPoint(x: 0, y: bottomCellOffset) @@ -132,6 +131,7 @@ class ViewDemoViewController: UIViewController,UITableViewDelegate,UITableViewDa } deinit { NotificationCenter.default.removeObserver(self) + print("123") } }