UIScrollView委托作为协议默认实现

时间:2019-04-18 03:28:35

标签: swift uiscrollview uicollectionview swift-protocols

我正在尝试为UIScrollViewDelegate方法实现默认实现:

func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>)

使用protocol

如果我将此方法放在类中,则称为它;但是,如果我尝试通过protocol用作默认实现,则永远不会调用它。

代码:

protocol DefaultScrollViewEndDragging {
    func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>)
}

extension DefaultScrollViewEndDragging {
    func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
        print("scrollViewWillEndDragging is called")
        // THIS IS NEVER CALLED
    }
}

class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, DefaultScrollViewEndDragging {

    @IBOutlet weak var collectionView: UICollectionView!

    override func viewDidLoad() {
        super.viewDidLoad()
        collectionView.dataSource = self
        collectionView.delegate = self
        collectionView!.register(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 5
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath)
        print("Cell \(indexPath.row)")
        cell.contentView.backgroundColor = .orange
        return cell
    }
}

我在做什么错了?

2 个答案:

答案 0 :(得分:1)

我认为您正在寻找类似的东西

protocol DefaultScrollViewEndDragging : UIScrollViewDelegate {
    func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>)
}

extension DefaultScrollViewEndDragging {
    func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
        print("scrollViewWillEndDragging is called")
        // THIS IS NEVER CALLED
    }
}

很遗憾,Obj-C无法访问协议扩展。因此不会被调用 scrollViewWillEndDragging

从笔记

  

同样,唯一的例外是协议扩展。不像任何   语言的其他构造,协议扩展方法是   在虚拟分派将要进行的情况下静态分派   导致不同的结果。没有编译器错误可防止这种不匹配。   https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20151207/001707.html

答案 1 :(得分:0)

更正代码

CASE 
    WHEN NetRate_Quote_Insur_Quote_Locat.[Transaction]='ADD' THEN 'Add'
    WHEN NetRate_Quote_Insur_Quote_Locat.[Transaction]='DELETED' THEN 'Delete'
    WHEN NetRate_Quote_Insur_Quote_Locat.[Transaction]='CHANGED' THEN 'Change'
    WHEN NetRate_Quote_Insur_Quote_Locat.[Transaction]='New Quote' THEN 'Add'
    WHEN tblQuotes.OriginalQuoteGuid IS NULL OR NetRate_Quote_Insur_Quote.TypeofBusiness = 'Reinstate' THEN 'Add'
    WHEN tblQuotes.QuoteStatusID = 12 THEN 'Delete' 
    ELSE 'No Change' 
END as TransactionName

}