iOS/Swift 어플 따라하기
[Swift] Xib파일을 사용하여 TableView에서 셀 사용자 지정
Chafle
2022. 9. 6. 11:16
반응형
CoCoaPod Class로 만들고
Subclass를 UITableViewCell로 -> XIB파일 생성한다.
UI를 설정해주고 난 뒤에
xib파일을 viewDidLoad에 등록하기
TableViewDataSource로 이동해서 dequeueReusableCell로 가져와야 되고
그렇게 하기 위해서는 클래스->클래스 캐스팅이 필요하다
reusableCell에서 MessageCell클래스를 캐스팅해야함
extension ChatViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return messages.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: K.cellIdentifier, for: indexPath)
as! MessageCell // superclass as! Subclass
cell.label.text = messages[indexPath.row].body
return cell
}
}
반응형