Multiple CollectionView inside the UITableView Cel

2019-08-24 14:55发布

问题:

I have Multiple CollectionView Inside the TableView and want to implement autoscrolling individually according to data count in each CollectionView

This code is working fine for single CollectionView

My UITableViewCell code is Here

class PromotionalOfferCell: UITableViewCell,UICollectionViewDelegate,UICollectionViewDataSource{

    @IBOutlet weak var collVwPromotionalOffer: UICollectionView!

    var x = 1
    var scroll_timer : Timer?

    var data_obj = [PromotionalOfferData]() {
        didSet {
            print(data_obj)
        }
    }

    override func awakeFromNib() {
        super.awakeFromNib()
        if collVwPromotionalOffer != nil{
            self.collVwPromotionalOffer.dataSource = self
            self.collVwPromotionalOffer.delegate = self
        }
    }

    func setTimer() {
        scroll_timer = Timer.scheduledTimer(timeInterval: 3.0, target: self, selector: #selector(self.autoScroll), userInfo: nil, repeats: true)
    }

    @objc func autoScroll() {
        if self.x < self.data_obj.count {
            let indexPath = IndexPath(item: x, section: 0)
            self.collVwPromotionalOffer.scrollToItem(at: indexPath, at: .centeredHorizontally, animated: true)
            self.x = self.x + 1

        } else {
            self.x = 0
            self.collVwPromotionalOffer.scrollToItem(at: IndexPath(item: 0, section: 0), at: .centeredHorizontally, animated: true)
        }
    }

}

// CollectionView Delegate & DataSource

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if data_obj.count > 0{
            return data_obj.count
        }
        return 0
    }

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ImageCell", for: indexPath) as? ImageCell

    cell?.imgView.sd_setImage(with:URL(string: data_obj[indexPath.row].image!), placeholderImage: UIImage(named: "cod_logo"), options: [.highPriority]) { (image, error, cashtype, url) in }

    return cell!
}

UITableView Delegate

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

    if tableViewItems.count > 0{
        return self.tableViewItems.count
    }
    return 0
 }

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        if let Promotional_Item = tableViewItems[indexPath.row] as? [PromotionalOfferData]{

            let cell = tableView.dequeueReusableCell(withIdentifier: "PromotionalOfferCell", for: indexPath) as! PromotionalOfferCell

            cell.data_obj = Promotional_Item
            cell.collVwPromotionalOffer.reloadData()

            if cell.scroll_timer != nil {
                cell.scroll_timer!.invalidate()
                cell.scroll_timer = nil
            }

            if Promotional_Item.count > 1{
                cell.setTimer()
            }
            return cell
        }
    }

My Output is Like this

The issue is that I print in Log and realize that the timer is called more than one time for each CollectionView

Due to this the memory size of Application is Continues increase and I already invalidate the timer

Can someone tell me where I am Wrong? Thanks in advance

回答1:

Hi Nikunj instead of making to many timers , make one timer and handle actions like scrolling in its selector instead for different cells cause for every table view cell timer interval is same ,also you can try do this on willdisplay and invalidate in willEndDisplaying!(though I don't like this to many timers). Hope it helped!