3

我有多个显示相同类型单元格的视图控制器。我想在这样的协议扩展中设置委托:

class ProductsViewController: UIViewController, ProductShowcase {
    //other properties
    @IBOutlet weak var productCollectionView: UICollectionView!
    var dataSource: DataSource!

    override func viewDidLoad() {
        super.viewDidLoad()

        setupDataSource()

        setupCollectionView()
    }

    func didSelectProduct(product: Product) {
        print(product)
    }

    //other functions
}

protocol ProductShowcase: UICollectionViewDelegate {
    var dataSource: DataSource! { get set }
    var productCollectionView: UICollectionView! { get }

    func didSelectProduct(product: Product)
}

extension ProductShowcase {
    func setupCollectionView() {
        productCollectionView.registerClass(ProductCollectionViewCell.self, forCellWithReuseIdentifier: "productCell")
        productCollectionView.dataSource = dataSource
        print(self) //prints ProductsViewController
        productCollectionView.delegate = self // 
        print(productCollectionView.delegate) //prints optional ProductsViewController
    }
}

extension ProductShowcase {
    //this delegate method is not called
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        didSelectProduct(dataSource.dataObjects[indexPath.row])
    }
}

当在其中didSelectItemAtIndexPath实现时ProductsViewController会被调用。有什么我错过的还是这是一种错误的方法?

4

1 回答 1

3

这是一个 Objective-C 互操作性限制。您不能像您想要的那样在协议扩展中实现带有可选功能的协议(来自 Objective-C 类型 UIKit 控件的委托和数据源等的协议)。您可以仅使用如下编写的协议的默认实现:

// No, @objc in the front of protocol. (i.e. objc-type protocol)
protocol X {
}
于 2016-11-04T12:59:16.717 回答