侧边栏覆盖协议问题

sidebar overlay protocol issues

所以我正在尝试实现一个覆盖我的 tableviewController 的边栏,但我收到一条错误消息,指出 class 边栏未确认协议委托...不知道为什么?!

错误:类型 'SideBar' 不符合协议 'FellowTravelersTableViewControllerDelegate'

感谢大家的帮助!

import UIKit

@objc protocol SideBarDelegate{
    func sideBarDidSelectButtonAtIndex(index:Int)
    optional func sideBarWillClose()
    optional func sideBarWillOpen()
}

class SideBar: NSObject, FellowTravelersTableViewControllerDelegate {

    let barWidth:CGFloat = 150.0
    let sideBarTableViewTopInset:CGFloat = 64.0
    let sideBarContainerView:UIView = UIView()
    let fellowTravelersTableViewController:FellowTravelersTableViewController = FellowTravelersTableViewController()
    let originView:UIView!

    var animator:UIDynamicAnimator!
    var delegate:SideBarDelegate?
    var isSideBarOpen:Bool = false

    override init() {
        super.init()
    }

    init(sourceView:UIView, menuItems:Array<String>){
        super.init()
        originView = sourceView
        fellowTravelersTableViewController.tableData = menuItems

        setupSideBar()

        animator = UIDynamicAnimator(referenceView: originView)

        let showGestureRecognizer:UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: "handleSwipe:")
        showGestureRecognizer.direction = UISwipeGestureRecognizerDirection.Right
        originView.addGestureRecognizer(showGestureRecognizer)

        let hideGestureRecognizer:UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: "handleSwipe:")
        hideGestureRecognizer.direction = UISwipeGestureRecognizerDirection.Left
        originView.addGestureRecognizer(hideGestureRecognizer)

    }


    func setupSideBar(){

        sideBarContainerView.frame = CGRectMake(-barWidth - 1, originView.frame.origin.y, barWidth, originView.frame.size.height)
        sideBarContainerView.backgroundColor = UIColor.clearColor()
        sideBarContainerView.clipsToBounds = false

        originView.addSubview(sideBarContainerView)

        let blurView:UIVisualEffectView = UIVisualEffectView(effect: UIBlurEffect(style: UIBlurEffectStyle.Light))
        blurView.frame = sideBarContainerView.bounds
        sideBarContainerView.addSubview(blurView)


        fellowTravelersTableViewController.delegate = self
        fellowTravelersTableViewController.tableView.frame = sideBarContainerView.bounds
        fellowTravelersTableViewController.tableView.clipsToBounds = false
        fellowTravelersTableViewController.tableView.separatorStyle = UITableViewCellSeparatorStyle.None
        fellowTravelersTableViewController.tableView.backgroundColor = UIColor.clearColor()
        fellowTravelersTableViewController.tableView.scrollsToTop  = false
        fellowTravelersTableViewController.tableView.contentInset = UIEdgeInsetsMake(sideBarTableViewTopInset, 0, 0, 0)

        fellowTravelersTableViewController.tableView.reloadData()

        sideBarContainerView.addSubview(fellowTravelersTableViewController.tableView)

    }


    func handleSwipe(recognizer:UISwipeGestureRecognizer){
        if recognizer.direction == UISwipeGestureRecognizerDirection.Left{
            showSideBar(false)
            delegate?.sideBarWillClose?()

        }else{
            showSideBar(true)
            delegate?.sideBarWillOpen?()
        }

    }


    func showSideBar(shouldOpen:Bool){
        animator.removeAllBehaviors()
        isSideBarOpen = shouldOpen

        let gravityX:CGFloat = (shouldOpen) ? 0.5 : -0.5
        let magnitude:CGFloat = (shouldOpen) ? 20 : -20
        let boundaryX:CGFloat = (shouldOpen) ? barWidth : -barWidth - 1


        let gravityBehavior:UIGravityBehavior = UIGravityBehavior(items: [sideBarContainerView])
        gravityBehavior.gravityDirection = CGVectorMake(gravityX, 0)
        animator.addBehavior(gravityBehavior)

        let collisionBehavior:UICollisionBehavior = UICollisionBehavior(items: [sideBarContainerView])
        collisionBehavior.addBoundaryWithIdentifier("sideBarBoundary", fromPoint: CGPointMake(boundaryX, 20), toPoint: CGPointMake(boundaryX, originView.frame.size.height))
        animator.addBehavior(collisionBehavior)

        let pushBehavior:UIPushBehavior = UIPushBehavior(items: [sideBarContainerView], mode: UIPushBehaviorMode.Instantaneous)
        pushBehavior.magnitude = magnitude
        animator.addBehavior(pushBehavior)


        let sideBarBehavior:UIDynamicItemBehavior = UIDynamicItemBehavior(items: [sideBarContainerView])
        sideBarBehavior.elasticity = 0.3
        animator.addBehavior(sideBarBehavior)

    }

    func sideBarControlDidSelectRow(indexPath: NSIndexPath) {
        delegate?.sideBarDidSelectButtonAtIndex(indexPath.row)
    }

}




/* TableViewController */
import UIKit


protocol FellowTravelersTableViewControllerDelegate {
    func sidebarControllerDidSelectRow(indexPath:NSIndexPath)


}

class FellowTravelersTableViewController: UITableViewController {

    var delegate:FellowTravelersTableViewControllerDelegate?
    var tableData: Array<String> = []



    // MARK: - Table view data source

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {

        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return tableData.count
    }


    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        var cell:UITableViewCell! = tableView.dequeueReusableCellWithIdentifier("travelersCell") as UITableViewCell!

        if cell == nil{
            cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "travelersCell")

            cell!.backgroundColor = UIColor.clearColor()
            cell!.textLabel?.textColor = UIColor.darkTextColor()

            let selectedView:UIView = UIView(frame: CGRect(x: 0, y: 0, width: cell.frame.size.width, height: cell.frame.size.height))
            selectedView.backgroundColor = UIColor.blackColor().colorWithAlphaComponent(0.3)

            cell!.selectedBackgroundView = selectedView

        }

        cell!.textLabel?.text = tableData[indexPath.row]

        // Configure the cell...

        return cell
    }

    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return 45

    }

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        delegate?.sidebarControllerDidSelectRow(indexPath)
    }


}

在您的协议中,您定义了:

protocol FellowTravelersTableViewControllerDelegate { func sidebarControllerDidSelectRow(indexPath:NSIndexPath) }

您必须在 SideBarclass 中实施 func sidebarControllerDidSelectRow(indexPath:NSIndexPath) 才能正常工作。