TestsTested | ✗ |
LangLanguage | SwiftSwift |
License | MIT |
ReleasedLast Release | Dec 2017 |
SwiftSwift Version | 4.0 |
SPMSupports SPM | ✗ |
Maintained by Taiki Suzuki.
You can configure ReusableCell without casting!
To run the example project, clone the repo, and run pod install
from the Example directory first.
If you install from pod, you have to write import ReuseCellConfigure
.
// LeftIconTableViewCell.swift
class LeftIconTableViewCell: UITableViewCell, ReusableViewProtocol {
typealias RegisterType = RegisterNib
}
// ViewController.swift
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: UITableViewCell?
let alphabet = String(describing: UnicodeScalar("A".unicodeScalars.first!.value + UInt32(indexPath.row))!)
switch indexPath.row % 2 {
case 0:
cell = tableView.dequeue(with: LeftIconTableViewCell.self) { cell in
cell.alphabetLabel.text = alphabet
cell.randomBackgoundColor()
}
case 1:
cell = tableView.dequeue(with: RightIconTableViewCell.self) { cell in
cell.alphabetLabel.text = alphabet
}
default:
cell = tableView.dequeueReusableCell(withIdentifier: "UITableViewCell")
}
return cell!
}
// ReusableHeaderView.swift
class LeftIconTableViewCell: UICollectionReusableView, ReusableViewProtocol {
typealias RegisterType = RegisterClass
}
// ViewController.swift
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
let reusableView: UICollectionReusableView? = nil
switch UICollectionView.ElementKind(rawValue: kind) {
case .Some(.Header):
return collectionView.dequeue(with: ReusableHeaderView.self, of: .Header, for: indexPath) { view in
view.backgroundColor = .redColor()
}
case .Some(.Footer):
return collectionView.dequeue(with: ReusableHeaderView.self, of: .Footer, for: indexPath) { view in
view.backgroundColor = .blueColor()
}
default:
return reusableView
}
}
ReuseCellConfigure is available through CocoaPods. To install
it, simply add the following line to your Podfile:
pod "ReuseCellConfigure"
If you’re using Carthage, simply add
ReuseCellConfigure to your Cartfile
:
github "marty-suzuki/ReuseCellConfigure"
Make sure to add ReuseCellConfigure.framework
to "Linked Frameworks and Libraries" and "copy-frameworks" Build Phases.
Taiki Suzuki, [email protected]
ReuseCellConfigure is available under the MIT license. See the LICENSE file for more info.