本文介绍了UITableView 单元格内的多个 CollectionView ( Autoscroll ) 问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 TableView 里面有多个 CollectionView 并且想要根据每个 CollectionView 中的数据计数单独实现自动滚动

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

此代码适用于单个 CollectionView

我的UITableViewCell代码在这里

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

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
        }
    }

我的输出是这样的

问题是我在日志中打印并意识到定时器被调用每个 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

推荐答案

嗨 Nikunj 而不是创建多个计时器,而是创建一个计时器并处理操作,例如在其选择器中滚动而不是针对不同的单元格,因为每个表格视图单元格计时器间隔是同样,您也可以尝试在 willdisplay 上执行此操作并在 willEndDisplaying 中失效!(尽管我不喜欢很多计时器).希望有帮助!

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!

这篇关于UITableView 单元格内的多个 CollectionView ( Autoscroll ) 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 06:37