"Missing argument for parameter 'for' in call"

"Missing argument for parameter 'for' in call"

在使用 swift3 编码时,我想使用自定义协议和泛型来重用集合视图单元格。我知道这是重用单元格的标准方式:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "TacoCell", for: indexPath) as? TacoCell {

        cell.configureCell(taco: ds.tacoArray[indexPath.row])

        return cell
    }

    return UICollectionViewCell()
}

但每次我尝试这样做时:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(forIndexPath: indexPath) as TacoCell
    cell.configureCell(taco: ds.tacoArray[indexPath.row])
    return cell
}

编译器抱怨我有一个"Missing argument for parameter 'for' in call"...在这种情况下参数是"forIndexPath"

仅供参考...

我有用于重用单元格和加载笔尖的自定义扩展。代码如下:

可重用视图 class

import UIKit

protocol ReusableView: class  { }

extension ReusableView where Self: UIView {

    static var reuseIdentifier: String {
        return String.init(describing: self)
    }
}

NibLoadableView class

import UIKit

protocol NibLoadableView: class { }

extension NibLoadableView where Self: UIView {

    static var nibName: String {
        return String.init(describing: self)
    }
}

这是我对 UICollectionView 的扩展

import UIKit

extension UICollectionView {
    func register<T: UICollectionViewCell>(_: T.Type) where T: ReusableView, T: NibLoadableView {

    let nib = UINib(nibName: T.nibName, bundle: nil)
    register(nib, forCellWithReuseIdentifier: T.reuseIdentifier)
}


func dequeueReusableCell<T: UICollectionViewCell>(forIndexPath indexPath: NSIndexPath) -> T where T: ReusableView {

    guard let cell = dequeueReusableCell(withReuseIdentifier: T.reuseIdentifier, for: indexPath as IndexPath) as? T else {
        fatalError("Could not dequeue cell with identifier: \(T.reuseIdentifier)")
    }

    return cell
    }
}

extension UICollectionViewCell: ReusableView { }

问题是您在 Swift 3.0 代码旁边还有一些 Swift 2.2 代码,编译器在尝试选择要调用的方法时会感到困惑,因为没有什么完全匹配。

您的 cellForItemAt 方法使用集合视图中的 IndexPath 调用您自己的 dequeueReusableCell() 扩展方法。但是,您编写的扩展方法希望收到一个 NSIndexPath,这是一个微妙的不同。

修改你的扩展方法,问题应该解决了:

func dequeueReusableCell<T: UICollectionViewCell>(forIndexPath indexPath: IndexPath) -> T where T: ReusableView {