Swift 3 tableView의 다른 등록 cell
코드의 역할
private static let defaultCellidentifier = String(describing: UITableViewCell.self)
private static let testCellidentifier = String(describing: TestTableViewCell.self)
tableView.register(UITableViewCell.self, forCellReuseIdentifier:HomeViewController.defaultCellidentifier);
tableView.register(UINib.init(nibName: "TestTableViewCell", bundle: nil), forCellReuseIdentifier: HomeViewController.testCellidentifier);
tableView.registerCell(cellTypes: [UITableViewCell.self,TestTableViewCell.self])
let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: TestTableViewCell.self), for: indexPath)
다음은 분류된 실현 코드이다
import Foundation
import UIKit
extension UITableView{
func registerCell(cellTypes:[AnyClass]){
for cellType in cellTypes {
let typeString = String(describing: cellType)
let xibPath = Bundle.init(for: cellType).path(forResource: typeString, ofType: "nib")
if xibPath==nil {
self.register(cellType, forCellReuseIdentifier: typeString);
}
else{
self.register(UINib.init(nibName: typeString, bundle: nil), forCellReuseIdentifier: typeString)
}
}
}
}