编译器不喜欢使用Swift的UIScrollViewDelegate [英] Compiler not liking UIScrollViewDelegate with Swift
问题描述
在将 UIScrollView
实现到 UITableViewCell
时,我收到错误。
I am getting an error, when implementing a UIScrollView
into a UITableViewCell
.
class MyItemTableViewCell: UITableViewCell, UIScrollViewDelegate {
...
var scrollView: UIScrollView = {
let scroll = UIScrollView()
scroll.showsHorizontalScrollIndicator = false
scroll.delegate = self
return scroll;
}()
...
func scrollViewDidScroll(scrollView: UIScrollView!) {
...
}
func scrollViewWillEndDragging(scrollView: UIScrollView!, velocity: CGPoint, targetContentOffset: CMutablePointer<CGPoint>) {
...
}
...
}
编译器在行上给出了一个错误 scroll.delegate = self
说类型MyItemTableViewCell不符合协议'UIScrollViewDelegate'
The compiler gives me an error on the line scroll.delegate = self
saying that the Type MyItemTableViewCell does not conform to protocol ‘UIScrollViewDelegate’
没关系这个事实,协议中的方法都记录为可选( https://developer.apple.com/library/prerelease/iOS/documentation/UIKit/Reference/UIScrollViewDelegate_Protocol/index.html ),但我实施了两个。
Never mind the fact, that the methods in the protocol are all documented as optional (https://developer.apple.com/library/prerelease/iOS/documentation/UIKit/Reference/UIScrollViewDelegate_Protocol/index.html), but I have two implemented.
编译器对我有什么要求?
What does the compiler want from me?
谢谢
推荐答案
在初始化循环期间,在初始化所有属性之前,self不可用。将其更改为:
During the init cycle, self isn't usable until all properties have been initialized. Change it to:
class MyItemTableViewCell: UITableViewCell, UIScrollViewDelegate {
var scrollView: UIScrollView
func scrollViewDidScroll(scrollView: UIScrollView!) {
}
func scrollViewWillEndDragging(scrollView: UIScrollView!, velocity: CGPoint, inout targetContentOffset: CGPoint) {
}
init() {
scrollView = UIScrollView()
scrollView.showsHorizontalScrollIndicator = false
super.init(style:UITableViewCellStyle.Default, reuseIdentifier:"cell")
scrollView.delegate = self
}
}
这篇关于编译器不喜欢使用Swift的UIScrollViewDelegate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!