All Projects → marty-suzuki → ReuseCellConfigure

marty-suzuki / ReuseCellConfigure

Licence: MIT License
You can configure ReusableCell without casting!

Programming Languages

swift
15916 projects
shell
77523 projects
ruby
36898 projects - #4 most used programming language
objective c
16641 projects - #2 most used programming language

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, [email protected]

License

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].