테이블 뷰에서 커스텀 셀을 재사용 큐(Reusable Queue)에서 관리하려면 먼저 테이블 뷰에 해당 커스텀 셀을 등록해주어야 한다.
register() 메서드 사용
tableView.**register(CustomCell.self, forCellReuseIdentifier: "CustomCell")**
static 상수로 지정해두면 관리 및 사용이 더 편리하다.
커스텀 테이블 뷰 셀
final class FriendListTableViewCell: UITableViewCell {
**static let cellIdentifier = "friendListTableViewCell"**
...(중략)...
}
재사용 셀 등록
override func viewDidLoad() {
super.viewDidLoad()
...(중략)...
**// 재사용 셀 등록
friendListView.friendListTableView.register(
FriendListTableViewCell.self,
forCellReuseIdentifier: FriendListTableViewCell.cellIdentifier
)**
friendListView.friendListTableView.dataSource = self
friendListView.friendListTableView.delegate = self
...(중략)...
}
let cell = tableView.dequeueReusableCell(
withIdentifier: "CustomCell",
for: indexPath
) as! CustomCell
// 셀 구성
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
**guard let cell = tableView.dequeueReusableCell(
withIdentifier: FriendListTableViewCell.cellIdentifier,
for: indexPath
) as? FriendListTableViewCell else {
return UITableViewCell()
}**
if let friend = friendList[indexPath.section][1] as? User {
cell.nicknameLabel.text = friend.nickname
// 친구 이미지 불러오기
if let imageURL = friend.imageURL {
loadProfileImage(urlString: imageURL) { friendImage in
if let friendImage = friendImage {
cell.profileImageView.image = friendImage
} else {
cell.profileImageView.image = UIImage(named: "profile")
}
}
}
}
let chevronImage = UIImageView(image: UIImage(systemName: "chevron.right"))
chevronImage.tintColor = UIColor.semantic.textBody
cell.accessoryView = chevronImage
return cell
}