3

プロトコル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)
        }
    }
}

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()
}
4

1 に答える 1

5

実行が終了すると、割り当てweekDelegateが解除されますviewDidLoad-強い参照は保持せず、ローカル変数のみを保持します。それへの参照を保持する唯一の他のクラスはUICollectionViewそれ自体(あなたの場合はcollectionViewWeeks)ですが、それを as としてUICollectionView定義します。delegateweak

参照を維持するには、次のようにします。

// 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()
}
于 2018-01-25T08:18:54.727 に答える