Swift3 &引用;缺少参数'的参数;对于';“随时待命”;
在使用swift3编码时,我想使用自定义协议和泛型来重用集合视图单元。我知道这是重复使用电池的标准方式:Swift3 &引用;缺少参数'的参数;对于';“随时待命”;,swift3,xcode8-beta6,Swift3,Xcode8 Beta6,在使用swift3编码时,我想使用自定义协议和泛型来重用集合视图单元。我知道这是重复使用电池的标准方式: func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "
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”
仅供参考
我有用于重用单元格和加载NIB的自定义扩展。代码如下:
可重用视图类
import UIKit
protocol ReusableView: class { }
extension ReusableView where Self: UIView {
static var reuseIdentifier: String {
return String.init(describing: self)
}
}
import UIKit
protocol NibLoadableView: class { }
extension NibLoadableView where Self: UIView {
static var nibName: String {
return String.init(describing: self)
}
}
NibLoadableView类
import UIKit
protocol ReusableView: class { }
extension ReusableView where Self: UIView {
static var reuseIdentifier: String {
return String.init(describing: self)
}
}
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 { }
导入UIKit
扩展UICollectionView{
func寄存器(uu:T.Type),其中T:ReusableView,T:NibLoadableView{
设nib=UINib(nibName:T.nibName,bundle:nil)
寄存器(nib,forCellWithReuseIdentifier:T.reuseIdentifier)
}
func dequeueReusableCell(forindepath:nsindepath)->T其中T:ReusableView{
guard let cell=dequeueReuseAbleCell(使用reuseIdentifier:T.reuseIdentifier,for:indexPath作为indexPath)作为?T else{
fatalError(“无法将标识符为\(T.reuseIdentifier)的单元格出列”)
}
返回单元
}
}
扩展UICollectionViewCell:ReusableView{}
问题在于,您的Swift 3.0代码旁边有一点Swift 2.2代码,编译器在尝试选择要调用的方法时会感到困惑,因为没有完全匹配的方法
您的cellForItemAt
方法使用集合视图中的indepath
调用您自己的dequeueReusableCell()
扩展方法。然而,您编写的扩展方法期望收到一个nsindepath
,这是一个微妙的不同
对此修改扩展方法,问题应得到解决:
func dequeueReusableCell<T: UICollectionViewCell>(forIndexPath indexPath: IndexPath) -> T where T: ReusableView {
func-dequeueReusableCell(forindepath-indepath:indepath)->T其中T:ReusableView{
您能否提供一个链接,介绍使用(仅)forIndexPath
的dequeueReusableCell
的描述?另外,您是否尝试过将forIndexPath
替换为for
?抱歉@Evert…我对UICollectionView进行了扩展,并使用了我的“覆盖”dequeueReusableCell
方法,该方法具有参数forIndexPath
。我的假设是,由于它被接受为扩展,我可以使用它来重用我的单元格。我还包括了上面的扩展代码。谢谢@twostrows!这似乎使它得以运行