1

在 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
}

编译器抱怨我有一个“调用中的参数'for'缺少参数”......在这种情况下,参数是“forIndexPath”

供参考...

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

可重用视图类

import UIKit

protocol ReusableView: class  { }

extension ReusableView where Self: UIView {

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

NibLoadableView 类

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 { }
4

1 回答 1

2

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

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

将您的扩展方法修改为此,问题应该得到解决:

func dequeueReusableCell<T: UICollectionViewCell>(forIndexPath indexPath: IndexPath) -> T where T: ReusableView {
于 2016-09-07T10:16:55.290 回答