iOS
[iOS] identifier를 다루는 세가지 방법
seongminmon
2024. 7. 10. 09:07
1. 문자열
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "TestTableViewCell", for: indexPath) as! TestTableViewCell
return cell
}
2. Type Property
class TestTableViewCell: UITableViewCell {
static let identifier = "TestTableViewCell"
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: TestTableViewCell.identifier, for: indexPath) as! TestTableViewCell
return cell
}
3. Protocol + Extension
protocol ReuseIdentifierProtocol {
static var identifier: String { get }
}
extension UITableViewCell: ReuseIdentifierProtocol {
static var identifier: String {
return String(describing: self)
}
}
셀을 새로 만들 때마다 identifier를 만들 필요가 없어진다!