我在UIScrollView
中实施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
上给出了一个错误,指出Type MyItemTableViewCell does not conform to protocol ‘UIScrollViewDelegate’
不要紧,事实上,协议中的方法都记录为可选(https://developer.apple.com/library/prerelease/iOS/documentation/UIKit/Reference/UIScrollViewDelegate_Protocol/index.html),但我实现了两个。
编译器对我有什么要求?
由于
答案 0 :(得分:3)
在初始化循环期间,在初始化所有属性之前,自身不可用。将其更改为:
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
}
}