ios 如何解决错误“viewController 与 swift 协议 UIScrollViewDelegate 的冗余一致性?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37896298/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 09:26:41  来源:igfitidea点击:

How to solve the error "Redundant conformance of viewController to protocol UIScrollViewDelegate in swift?

iosswiftuitableviewuiscrollview

提问by Harish Singh

I am new on stackOverflow and learning swift. I am getting the error "Redundant conformance of viewController to protocol while working with Stretch headers.UIScrollViewDelegate. I am specifying my code below. please correct any one .

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

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          
    }

回答by Anil Varghese

You are getting this error because your class ViewControllerconforming to the protocol UIScrollViewDelegatein two ways. UITableViewControlleris already conforming to that protocol, you dont need to add it again. So remove UIScrollViewDelegatefrom there, you are good.

您收到此错误是因为您的类ViewControllerUIScrollViewDelegate两种方式符合协议。UITableViewController已经符合该协议,您不需要再次添加它。所以UIScrollViewDelegate从那里移开,你很好。

This is how it is UITableViewControllerconforms to UITableViewDelegatewhich conforms to UIScrollViewDelegate. This would be enough

这就是它如何UITableViewController符合 UITableViewDelegate哪个符合UIScrollViewDelegate。这就足够了

class ViewController: UITableViewController{
}

回答by Akshansh Thakur

The shortest explanation would be that Your UITableViewController comes with a built in scroll View, so you don'tneed a delegate for Scrollview. Remove UIScrollViewDelegateand you will be fine.

最短的解释是你的 UITableViewController 带有一个内置的滚动视图,所以你不需要滚动视图的委托。删除UIScrollViewDelegate,你会没事的。

Feel free to ask any question :)

随时提出任何问题:)