         case .vertical:
             index = Int((collectionView.contentOffset.y + layout.itemSize.height * 0.5) / layout.itemSize.height)
         }
-        return index
+        return max(0, index)
+    }
+    
+    override public func willMove(toSuperview newSuperview: UIView?) {
+        super.willMove(toSuperview: newSuperview)
+        if newSuperview != nil {
+            deinitTimer()
+        }
+    }
+    
+    deinit {
+        collectionView.delegate = nil
+        collectionView.dataSource = nil
+        deinitTimer()
     }
 }
 
 extension AutoCycleAdview: UICollectionViewDelegate, UICollectionViewDataSource {
     
-    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
+    public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
         return itemsCount
     }
     
-    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
+    public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
         let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! AdCell
         let idx = index(with: indexPath.row)
         let uri = imagUrls[idx]

        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .lightGray
        setUpCollectinView()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func initialization() {
        
    }
    
    func setUpCollectinView() {

        }
    }
    
    public var titles: [String] = [] {
        didSet {
            if onlyDisplayText {
                imagUrls = titles
