编译器不喜欢使用Swift的UIScrollViewDelegate

时间:2014-06-11 14:48:04

标签: ios swift

我在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),但我实现了两个。

编译器对我有什么要求?

由于

1 个答案:

答案 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
    }
}