gpt4 book ai didi

iOS Swift CollectionView 委托(delegate)未被调用

转载 作者:可可西里 更新时间:2023-11-01 00:55:56 24 4
gpt4 key购买 nike

我已经将协议(protocol) UICollectionViewDelegate、UICollectionViewDelegateFlowLayout、UIScrollViewDelegate 整合到一个名为 CollectionViewWeekDelegate 的单独类中,但是诸如 didSelectItemAt 之类的委托(delegate)方法没有得到打电话。

我还在 viewDidLoad() 中将委托(delegate)设置为 CollectionView

请在下面找到代码:

委托(delegate)类

class CollectionViewWeekDelegate: NSObject, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, UIScrollViewDelegate {

internal var parent: EarningsViewController?

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

parent?.weekTabSelectedPosition = indexPath.item

let indexPathh = IndexPath(item: indexPath.item, section: 0)

parent?.collectionViewSchedule.scrollToItem(at: indexPathh, at: .right, animated: true)
parent?.collectionViewWeeks.scrollToItem(at: indexPath, at: .top, animated: true)

parent?.collectionViewWeeks.reloadData()

}


func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {

let offsetX = parent?.collectionViewSchedule.contentOffset.x
let contentWidth = parent?.collectionViewSchedule.bounds.width

let page = Int(offsetX! / contentWidth!)

parent?.weekTabSelectedPosition = page



var earningsText = "\("grossEarnings".localize()) $\(String(format: "%0.2f", 0.0))"

if let earnings = parent?.schedules?[page].grossWeekSalary{
earningsText = "\("grossEarnings".localize()) $\(String(format: "%0.2f", earnings))"


parent?.buttonGrossEarnings.setTitle(earningsText, for: .normal)

parent?.collectionViewWeeks.selectItem(at: IndexPath(item: page, section: 0), animated: true, scrollPosition: .centeredHorizontally)}
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
if collectionView.tag == 2{
return CGSize(width: UIScreen.main.bounds.width, height: (parent?.collectionViewSchedule.bounds.height)!)
}else{
return CGSize(width: 150, height: 50)
}
}
}

分配 CollectionView 委托(delegate)的 viewDidLoad 函数:

override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.title = "schedule".localize()

self.navigationItem.leftBarButtonItem = super.getMenuBartItem()

if let flowLayout = collectionViewWeeks.collectionViewLayout as? UICollectionViewFlowLayout{
flowLayout.estimatedItemSize = CGSize(width: 150, height: 50)
}

if let collectionFlowLayout = collectionViewSchedule.collectionViewLayout as? UICollectionViewFlowLayout{
collectionFlowLayout.minimumLineSpacing = 0
collectionFlowLayout.minimumInteritemSpacing = 0

}

let weekDelegate = CollectionViewWeekDelegate()
weekDelegate.parent = self

collectionViewWeeks.delegate = weekDelegate
collectionViewSchedule.delegate = self

collectionViewWeeks.reloadData()
}

最佳答案

viewDidLoad 完成执行时,您的 weekDelegate 将被释放 - 您不会保留对它的强引用,只有局部变量。唯一持有对它的引用的其他类是 UICollectionView 本身(在您的例子中是 collectionViewWeeks),但是 UICollectionView 定义了它的 delegate 作为

只需这样做即可使引用保持事件状态:

// create a strong property and use it instead of local variable
fileprivate let weekDelegate = CollectionViewWeekDelegate()

override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.title = "schedule".localize()

self.navigationItem.leftBarButtonItem = super.getMenuBartItem()

if let flowLayout = collectionViewWeeks.collectionViewLayout as? UICollectionViewFlowLayout{
flowLayout.estimatedItemSize = CGSize(width: 150, height: 50)
}

if let collectionFlowLayout = collectionViewSchedule.collectionViewLayout as? UICollectionViewFlowLayout{
collectionFlowLayout.minimumLineSpacing = 0
collectionFlowLayout.minimumInteritemSpacing = 0

}

weekDelegate.parent = self

collectionViewWeeks.delegate = weekDelegate
collectionViewSchedule.delegate = self

collectionViewWeeks.reloadData()
}

关于iOS Swift CollectionView 委托(delegate)未被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48437359/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com