1) 문제가 발생하면 각 통화시 항상 새 UILabel
을 셀에 추가해야합니다. 당신은 3D 디버그에서 볼 수 있습니다 당신은 셀
3) 내가 셀의 두 개 이상의 유형이의 초기화에 한 번만 파단 및 레이블을 만들어야합니다) 더 많은 UILabel의이
2
을 적층되어 있음을 볼 수 테이블에 표시됩니다. 난 호출 tableViewController에서 세포의 내부 구현을 decouple 만들었습니다. 지금 당신은 다른 세포를 만들 무료이며 각 셀 콘텐츠의 오른쪽 레이아웃 및 업데이트에 대한 책임 :
class MyTinyTableViewCell: UITableViewCell, UpdateableTableViewCell {
let cellTitleLabel = UILabel()
//[...]
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
setup()
}
func setup() {
cellTitleLabel.frame = : CGRect(x: 45,y: 2,width: 100,height: 20))
cellTitleLabel.textAlignment = NSTextAlignment.left
cellTitleLabel.font = UIFont(name: "Swiss721BT-Roman", size: 16)
self.contenView.addSubview(cellTitleLabel)
}
// implement protocol UpdateableTableViewCell
func configCell(with data:MyTableDataModel){
resetCell()
cellTitleLabel.text = data.description
}
///[...]
func resetCell(){
// reset here all other cell ui elements to default values
}
}
지금 내가 다른 재사용 할 수 있습니다 : 지금 내 모든 다른 세포가이 프로토콜을 구현
protocol UpdateableTableViewCell {
func configCell(with data:MyTableDataModel)
}
을
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellData:MyTableDataModel = myDataArray(indexPath.row)
// calculate the celltype depending on the current data in the cell
let cellTypeIdentifier = getCellTypeIdentifier(cellData)
if let cell = tableView.dequeueReusableCell(withIdentifier: cellTypeIdentifier, for: indexPath) as UpdateableTableViewCell {
cell.configCell(with: cellData)
}
}
하고있는 tableView에서 모든 세포를 등록하는 것을 잊지 마세요 :
한 단계와 세포
tableView.register(MyTinyTableViewCell.self, forCellReuseIdentifier: "tinyCell")
tableView.register(MyBigTableViewCell.self, forCellReuseIdentifier: "bigCell")
tableView.register(MyImageTableViewCell.self, forCellReuseIdentifier: "imageCell")
4) 데이터 모델을 추상화하여 프로토콜을 변경하면 셀에 다른 모델을 사용할 수 있습니다.
protocol MyTableDataModel {
let description:String { get }
let title:String { get }
let imageUrl:String { get }
}
커스텀 서브 클래스 'UITableViewCell'을 생성하지 않으시겠습니까? – Pierce