如何在swift中解决错误" viewController与协议UIScrollViewDelegate的冗余一致性?

时间:2016-06-18 11:12:28

标签: ios swift uitableview uiscrollview

我是stackOverflow的新手并且学习swift。我收到了错误  "使用Stretch headers时,viewController与协议的冗余一致性.UIScrollViewDelegate。我在下面指定我的代码。请更正任何一个。

class ViewController: UITableViewController , UIScrollViewDelegate {
    private let kTableHeaderHeight : CGFloat = 300.0
    // Using Implicitly Unwrapped Optional, UIView!
    var headerView:UIView!

    let items = [
    NewsItem(category: .World, summary: "Climate Change protests, Need to preserve our Ecosysytem"),
    NewsItem(category: .India, summary: "Climate Change protests, Need to preserve our Ecosysytem"),
    NewsItem(category: .America, summary: "Climate Change protests,Need to preserve our Ecosysytem"),
    NewsItem(category: .Japan, summary: "Climate Change protests, Need to preserve our Ecosysytem"),
    NewsItem(category: .China, summary: "Climate Change protests, Need to preserve our Ecosysytem"),
    NewsItem(category: .Nepal, summary: "Climate Change protests, Need to preserve our Ecosysytem")]
    override func viewDidLoad() {
        super.viewDidLoad()
        headerView = tableView.tableHeaderView
        tableView.tableHeaderView = nil
        tableView.addSubview(headerView)
   tableView.contentInset = UIEdgeInsetsMake(kTableHeaderHeight, 0, 0, 0)
        tableView.contentOffset = CGPointMake(0, -kTableHeaderHeight)
        updateHeaderView()
    }
    func updateHeaderView(){
        var HeaderRect = CGRectMake(0,-kTableHeaderHeight, tableView.bounds.width, kTableHeaderHeight)

        if tableView.contentOffset.y < -kTableHeaderHeight{
            HeaderRect.origin.y = tableView.contentOffset.y
            HeaderRect.size.height = -tableView.contentOffset.y
        }
        headerView.frame = HeaderRect
    }
    override func didReceiveMemoryWarning() {enter code here
        super.didReceiveMemoryWarning()   
    }
    override func scrollViewDidScroll(scrollView: UIScrollView) {
        updateHeaderView()
    }
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return items.count
    }
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let item = items[indexPath.row]
let cell = tableView.dequeueReusableCellWithIdentifier("Cell" forIndexPath: indexPath) as! NewsItemTableViewCell
        cell.newsItem = item
 return cell          
    }

2 个答案:

答案 0 :(得分:10)

您收到此错误是因为您的班级ViewController在两个方面符合协议UIScrollViewDelegateUITableViewController已经符合该协议,您不需要再次添加它。所以从那里删除UIScrollViewDelegate,你很好。

这是UITableViewController符合UITableViewDelegate符合UIScrollViewDelegate的方式。这就足够了

class ViewController: UITableViewController{
}

答案 1 :(得分:3)

最简单的解释是你的UITableViewController带有一个内置的滚动视图,因此 需要Scrollview的委托。删除UIScrollViewDelegate,你会没事的。

随意提出任何问题:)

相关问题