【问题标题】:Multiple CollectionView inside the UITableView Cell ( Autoscroll ) issueUITableView Cell ( Autoscroll ) 内的多个 CollectionView 问题
【发布时间】:2019-04-22 05:49:26
【问题描述】:

我在TableView 中有多个CollectionView,并希望根据每个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
        }
    }

我的输出是这样的

问题是我在 Log 中打印并意识到调用了计时器 每个CollectionView不止一次

因此,应用程序的内存大小不断增加,我 已经invalidate计时器了

谁能告诉我我哪里错了?提前致谢

【问题讨论】:

  • tableviewcell 将被重用。我认为在 cellWillApprear 中处理并消失会很好
  • 您发布的代码似乎没有问题。你也可以显示UICollectionViewDelegate, UICollectionViewDataSource 吗?
  • @Adeel 在委托中也没有任何问题,因为它可能只加载图像。即使是我在问题中添加的。
  • awakeFromNib 仅被调用一次,因此不可能多次实例化计时器。你能在PromotionalOfferCell 出列并设置data_obj 数组的位置显示UITableViewDataSource 吗?
  • @Adeel 正如你所说,在我的问题中添加 UITableViewDataSource

标签: ios swift uitableview uicollectionview autoscroll


【解决方案1】:

嗨 Nikunj 与其制作多个计时器,不如制作一个计时器并处理诸如在其选择器中滚动而不是针对不同单元格的操作,因为每个表格视图单元格计时器间隔相同,您也可以尝试在 willdisplay 上执行此操作,并在 willEndDisplaying 中无效!(虽然我不喜欢很多计时器)。希望对您有所帮助!

【讨论】:

  • 实际上,我在每个集合视图中都有不同数量的项目,并且希望为所有从当前索引到下一个索引的所有项目单独设置自动滚动,那么我该如何使用单个计时器来做到这一点?
  • 你是说从 Uitableview cellForRowAt 方法调用 self.setTimer()
  • 在您的控制器中调用 setTimer,在您的控制器中您知道哪些单元格必须滚动!简单或者你可以在控制器中创建一个函数来计算内部可见表格单元格collectionview是否需要根据数据滚动然后执行它,我认为这更好,希望你得到它!
  • 按照,你说我从 Uitableview cellForRowAt 方法调用 self.setTimer() 并且也从那里无效而不是 awakeFromNib 一切都很好获取数据以显示一个或多个集合视图,但是在单击下一个类别时,当时没有任何数据显示集合视图,以前的计时器不会失效,所以我怎么能使其失效
  • 1.您的计时器对象和计时器选择器将在您的 VC 中。 2.在 VC 中的选择器中,您将相应地管理滚动(在 willDisplay 中)。 3.添加处理来自VC 4的单元格计数器的函数。因为时间间隔对所有人来说都是相同的,所以这应该不是问题,同时如果我有时间我也会添加代码,
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-03
  • 1970-01-01
相关资源
最近更新 更多