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

问题描述 投票:0回答:1

我在CollectionView内有多个TableView,并希望根据每个CollectionView中的数据计数单独实施自动滚动

此代码适用于单个CollectionView

我的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代表

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

我的输出是这样的

enter image description here

问题是我在Log中打印并意识到每个CollectionView都会调用一次以上的计时器

由于这个应用程序的内存大小继续增加,我已经invalidate计时器

有人能告诉我哪里错了吗?提前致谢

ios swift uitableview uicollectionview autoscroll
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.