【问题标题】:How to solve the error "Redundant conformance of viewController to protocol UIScrollViewDelegate in swift?如何快速解决错误“viewController 与协议 UIScrollViewDelegate 的冗余一致性?
【发布时间】:2016-10-20 03:52:08
【问题描述】:

我是 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          
    }

【问题讨论】:

    标签: ios swift uitableview uiscrollview


    【解决方案1】:

    您收到此错误是因为您的类 ViewController 以两种方式符合协议 UIScrollViewDelegate。 UITableViewController 已经符合该协议,您无需再次添加。所以从那里删除UIScrollViewDelegate,你很好。

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

    class ViewController: UITableViewController{
    }
    

    【讨论】:

    • 非常感谢
    • 也为我工作!谢谢,顺便说一句,它也在firebase示例代码here中给出
    【解决方案2】:

    最简单的解释是您的 UITableViewController 带有一个内置的滚动视图,因此您不需要 滚动视图的委托。去掉UIScrollViewDelegate就没事了。

    有任何问题欢迎提问:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-07-11
      • 1970-01-01
      • 2016-03-21
      • 1970-01-01
      • 2015-11-16
      • 2015-12-09
      • 2016-03-10
      相关资源
      最近更新 更多