简体   繁体   English

UITableView Cell(Autoscroll)问题中的多个CollectionView

[英]Multiple CollectionView inside the UITableView Cell ( Autoscroll ) issue

I have Multiple CollectionView Inside the TableView and want to implement autoscrolling individually according to data count in each CollectionView 我在TableView有多个CollectionView ,并希望根据每个CollectionView数据计数单独实现自动滚动

This code is working fine for single CollectionView 此代码适用于单个CollectionView

My UITableViewCell code is Here 我的UITableViewCell代码就在这里

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 UITableView委托

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 问题是我在Log中打印并意识到每个CollectionView都会调用多次计时器

Due to this the memory size of Application is Continues increase and I already invalidate the timer 因此,应用程序的内存大小会持续增加,并且我已经invalidate计时器invalidate

Can someone tell me where I am Wrong? 有人能告诉我哪里错了吗? Thanks in advance 提前致谢

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). 嗨Nikunj而不是制作多个计时器,制作一个计时器并处理其选择器中的滚动操作,而不是针对不同的单元格导致每个表视图单元格计时器间隔相同,也可以尝试在willdisplay上执行此操作并在willEndDisplaying中使其无效!我对很多计时器都不喜欢这样。 Hope it helped! 希望它有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM