Aks*_*ood 3 collectionview ios uicollectionviewdelegate swift
我已经将协议符合UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, UIScrollViewDelegate一个单独的类,CollectionViewWeekDelegate但didSelectItemAt没有调用诸如此类的委托方法。
我委托也设置CollectionView在viewDidLoad()
请在下面找到代码:
委托类
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)
}
}
}
Run Code Online (Sandbox Code Playgroud)
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()
}
Run Code Online (Sandbox Code Playgroud)
执行完成后,您weekDelegate将被解除分配viewDidLoad- 您不会保留对它的强引用,只保留局部变量。持有对它的引用的唯一其他类是它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()
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3256 次 |
| 最近记录: |