iOS Swift CollectionView 委托未被调用
iOS Swift CollectionView delegate not getting called
我已将协议 UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, UIScrollViewDelegate
与名为 CollectionViewWeekDelegate
的单独 class 相一致,但未调用诸如 didSelectItemAt
之类的委托方法。
我还在 viewDidLoad()
中将委托设置为 CollectionView
请找到下面的代码:
代表Class
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
委托的 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()
}
你的 weekDelegate
在 viewDidLoad
完成执行时被释放 - 你没有保留对它的强引用,只有局部变量。唯一持有对它的引用的其他 class 是 UICollectionView
本身(在您的情况下 collectionViewWeeks
),但是 UICollectionView
将其 delegate
定义为 weak
.
只是为了让引用保持活跃:
// 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()
}
我已将协议 UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, UIScrollViewDelegate
与名为 CollectionViewWeekDelegate
的单独 class 相一致,但未调用诸如 didSelectItemAt
之类的委托方法。
我还在 viewDidLoad()
CollectionView
请找到下面的代码:
代表Class
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
委托的 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()
}
你的 weekDelegate
在 viewDidLoad
完成执行时被释放 - 你没有保留对它的强引用,只有局部变量。唯一持有对它的引用的其他 class 是 UICollectionView
本身(在您的情况下 collectionViewWeeks
),但是 UICollectionView
将其 delegate
定义为 weak
.
只是为了让引用保持活跃:
// 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()
}