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
}
나는 세포와로드 펜촉을 재사용에 대한 사용자 정의 확장이 forIndexPath "참고로
...
"...이 경우 매개 변수입니다. "
는ReusableView 클래스
당신은있어import UIKit
protocol ReusableView: class { }
extension ReusableView where Self: UIView {
static var reuseIdentifier: 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 { }
당신이 (만) 'forIndexPath'를 사용 dequeueReusableCell''의 설명에 대한 링크를 제공 할 수 있을까요? 또한'forIndexPath'를'for'로 대체 해 보았습니까? – Evert
Sorry @Evert ... UICollectionView의 확장을 만들었고,'forIndexPath' 인자를 가진 "재정의 된"'dequeueReusableCell' 메소드를 사용하고 있습니다. 내 가정은 그것이 확장으로 받아 들여 졌기 때문에 그것을 사용하여 셀을 재사용 할 수있었습니다. 위의 확장 코드도 포함했습니다. – CommittedEel