Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 .

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          
    }
like image 716
Harish Singh Avatar asked Jun 18 '16 11:06

Harish Singh


2 Answers

You are getting this error because your class ViewController conforming to the protocol UIScrollViewDelegate in two ways. UITableViewController is already conforming to that protocol, you dont need to add it again. So remove UIScrollViewDelegatefrom there, you are good.

This is how it is UITableViewController conforms to UITableViewDelegate which conforms to UIScrollViewDelegate. This would be enough

class ViewController: UITableViewController{
}
like image 123
Anil Varghese Avatar answered Oct 21 '22 23:10

Anil Varghese


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

Feel free to ask any question :)

like image 37
Akshansh Thakur Avatar answered Oct 21 '22 22:10

Akshansh Thakur