테이블 뷰 셀 구조
Edit/Content View/Accessory View로 구성
System Cell Styles
: Custom, Basic, Right detail, Left Detail, Subtitle
✅ Left Detail은 img 불가능
셀 검색 2가지 방법
//1번
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = list[indexPath.row]
cell.imageView?.image = UIImage(systemName: "star")
return cell
}
//2번
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let cell = sender as? UITableViewCell{
if let indexpath = listTableview.indexPath(for: cell){
if let vc = segue.destination as? DetailViewController{
vc.value = list[indexpath.row]
}
}
}
}
Cocoa Touch Class
https://babbab2.tistory.com/51
: 높이를 미리 예측할 수 없을때 사용
//코드로 설정
override func viewDidLoad() {
super.viewDidLoad()
listTableView.rowHeight = UITableViewAutomaticDimension
listTableView.estimatedRowHeight = UITableViewAutomaticDimension
}
//indexpath 별로 설정해야 할때
extension SelfSizingCellViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if indexPath.row == 0{
return 100
}
return UITableViewAutomaticDimension
}
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
if indexPath.row == 0{
return 100
}
return UITableViewAutomaticDimension
}
}
: Custom Cell은 다른 스타일과 다르게 제약을 직접 추가하고 Outlet을 직접 연결해야한다.
✅ label들을 viewController와 연결하면 몇번째 cell에 포함되어 있는 label에 접근해야하는지 알 수 없습니다. 그러므로,
//TimeTableViewCell : custom cell class
class TimeTableViewCell: UITableViewCell {
@IBOutlet weak var dateLabel: UILabel!
@IBOutlet weak var locationLabel: UILabel!
@IBOutlet weak var ampmLabel: UILabel!
@IBOutlet weak var timeLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
Self-Sizing Cell에서 높이가 올바르게 계산되도록 제약 구성.
prototype Cell을 구성할때 설정한 높이는 runtime에서는 다른 값보다 우선순위가 낮습니다. Self Sizing이 활성화 되어있다면 자동으로 계산되고, 나머지 경우에는 tableView에 설정되어 있는 높이나 delegate가 리턴하는 높이로 표시됩니다.
✅ 그래서 원하는 높이로 계산되려면 label과 cell사이에 top과 bottom 제약을 설정해줘야한다
"반복적으로 사용할 수 있는" 테이블 뷰 셀을 구현해보자!
empty file에 prototype cell을 만들어두고 코드로 table view에 연결. 주로 ViewDidLoad에서 구현한다.
✅ 코드로 identifer를 설정할꺼기 때문에 storyboard에서 identifer는 공백으로 해둬야한다.
✅ 반복적으로 사용되는 cell에 수정사항이 있을때 편리하다.
override func viewDidLoad() {
super.viewDidLoad()
let cellnib = UINib(nibName: "SharedCustomCell", bundle: nil)
listTableview.register(cellnib, forCellReuseIdentifier: "SharedCustomCell")
}