reuseIdentifier improvements

This commit is contained in:
Max Sokolov 2016-08-20 14:20:29 +03:00
parent a9c5d997cb
commit d42757fb68
6 changed files with 24 additions and 33 deletions

View File

@ -20,35 +20,26 @@
import UIKit
public protocol ReusableCell {
static func reusableIdentifier() -> String
static func nib() -> UINib?
}
public protocol ConfigurableCell: ReusableCell {
public protocol ConfigurableCell {
associatedtype T
static var reuseIdentifier: String { get }
static var estimatedHeight: CGFloat? { get }
static var defaultHeight: CGFloat? { get }
func configure(with _: T)
}
public extension ReusableCell where Self: UITableViewCell {
static func reusableIdentifier() -> String {
return String(self)
}
static func nib() -> UINib? {
return nil
}
}
public extension ConfigurableCell where Self: UITableViewCell {
static var reuseIdentifier: String {
get {
return String(self)
}
}
static var estimatedHeight: CGFloat? {
get {
return UITableViewAutomaticDimension

View File

@ -49,7 +49,7 @@ public class PrototypeHeightStrategy: CellHeightCalculatable {
return height
}
guard let cell = tableView.dequeueReusableCellWithIdentifier(row.reusableIdentifier) else { return 0 }
guard let cell = tableView.dequeueReusableCellWithIdentifier(row.reuseIdentifier) else { return 0 }
cell.bounds = CGRectMake(0, 0, tableView.bounds.size.width, cell.bounds.height)

View File

@ -29,16 +29,16 @@ public class TableCellManager {
self.tableView = tableView
}
public func register(cellType cellType: AnyClass, forReusableCellIdentifier reusableIdentifier: String) {
public func register(cellType cellType: AnyClass, forCellReuseIdentifier reuseIdentifier: String) {
if registeredIds.contains(reusableIdentifier) {
if registeredIds.contains(reuseIdentifier) {
return
}
// check if cell is already registered, probably cell has been registered by storyboard
if tableView?.dequeueReusableCellWithIdentifier(reusableIdentifier) != nil {
if tableView?.dequeueReusableCellWithIdentifier(reuseIdentifier) != nil {
registeredIds.insert(reusableIdentifier)
registeredIds.insert(reuseIdentifier)
return
}
@ -46,13 +46,13 @@ public class TableCellManager {
// we hope that cell's xib file has name that equals to cell's class name
// in that case we could register nib
if let _ = bundle.pathForResource(reusableIdentifier, ofType: "nib") {
tableView?.registerNib(UINib(nibName: reusableIdentifier, bundle: bundle), forCellReuseIdentifier: reusableIdentifier)
if let _ = bundle.pathForResource(reuseIdentifier, ofType: "nib") {
tableView?.registerNib(UINib(nibName: reuseIdentifier, bundle: bundle), forCellReuseIdentifier: reuseIdentifier)
// otherwise, register cell class
} else {
tableView?.registerClass(cellType, forCellReuseIdentifier: reusableIdentifier)
tableView?.registerClass(cellType, forCellReuseIdentifier: reuseIdentifier)
}
registeredIds.insert(reusableIdentifier)
registeredIds.insert(reuseIdentifier)
}
}

View File

@ -123,9 +123,9 @@ public class TableDirector: NSObject, UITableViewDataSource, UITableViewDelegate
let row = sections[indexPath.section].items[indexPath.row]
cellManager?.register(cellType: row.cellType, forReusableCellIdentifier: row.reusableIdentifier)
cellManager?.register(cellType: row.cellType, forCellReuseIdentifier: row.reuseIdentifier)
let cell = tableView.dequeueReusableCellWithIdentifier(row.reusableIdentifier, forIndexPath: indexPath)
let cell = tableView.dequeueReusableCellWithIdentifier(row.reuseIdentifier, forIndexPath: indexPath)
if cell.frame.size.width != tableView.frame.size.width {
cell.frame = CGRectMake(0, 0, tableView.frame.size.width, cell.frame.size.height)

View File

@ -38,7 +38,7 @@ public protocol RowHashable {
public protocol Row: RowConfigurable, RowActionable, RowHashable {
var reusableIdentifier: String { get }
var reuseIdentifier: String { get }
var cellType: AnyClass { get }
var estimatedHeight: CGFloat? { get }
@ -54,8 +54,8 @@ public class TableRow<ItemType, CellType: ConfigurableCell where CellType.T == I
return ObjectIdentifier(self).hashValue
}
public var reusableIdentifier: String {
return CellType.reusableIdentifier()
public var reuseIdentifier: String {
return CellType.reuseIdentifier
}
public var estimatedHeight: CGFloat? {

View File

@ -53,7 +53,7 @@ class TestTableViewCell: UITableViewCell, ConfigurableCell {
return TestTableViewCellOptions.EstimatedHeight
}
static func reusableIdentifier() -> String {
static var reuseIdentifier: String {
return TestTableViewCellOptions.ReusableIdentifier
}