Skip to content
This repository has been archived by the owner on Jul 1, 2020. It is now read-only.

marty-suzuki/ReuseCellConfigure

Repository files navigation

ReuseCellConfigure

Platform Language Version Carthage compatible License

You can configure ReusableCell without casting!

Usage

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

Requirements

  • Xcode 9.1 or greater
  • iOS 8.0 or greater

Installation

CocoaPods

ReuseCellConfigure is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "ReuseCellConfigure"

Carthage

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.

Author

Taiki Suzuki, s1180183@gmail.com

License

ReuseCellConfigure is available under the MIT license. See the LICENSE file for more info.