Cell + Section ➡️ Table View
Cell
Table View
Section
Cell을 그룹화할 수 있음! 이 앞에 붙는 타이틀이 섹션이라고 보면 됨~
테이블뷰가 몇 개의 셀로 구성되어 있는지 우리에게 알려주는 역할이 아니라, 몇 개의 셀을 생성해야 할지 iOS 시스템에게 알려주기 위해 작성
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return section == 0 ? 2 : list.count
}
씬에 표현해야 할 셀의 수만큼 반복적으로 호출
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "memoCell") else {
return UITableViewCell()
}
// 섹션에 따라 다른 디자인
if indexPath.section == 0 {
cell.textLabel?.text = "첫번째 섹션입니다 - \(indexPath)"
cell.textLabel?.textColor = .brown
cell.textLabel?.font = .boldSystemFont(ofSize: 15)
} else {
cell.textLabel?.text = list[indexPath.row]
cell.textLabel?.textColor = .blue
cell.textLabel?.font = .italicSystemFont(ofSize: 13)
}
return cell
}
➡️ 명확하게 명시하는 것이 좋다.
코드로 확실히 구현해주는 편
고정으로 설정할 수도 있고, 디바이스별로 높이 기준으로 20% 높이를 주고싶어~ 도 가능
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
// 섹션별로, 행 번호대로 높이를 조절해줄 수 있음
return indexPath.row == 0 ? 44 : 80
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 2
}
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return "섹션 타이틀"
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("셀 선택~")
}
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if indexPath.section == 1 {
if editingStyle == .delete {
list.remove(at: indexPath.row)
tableView.reloadData()
}
}
}
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return indexPath.section == 0 ? false : true
}
특정 섹션의 특정 행에 대한 위치 정보(좌표 정보)
섹션과 행의 속성을 통해 엑세스 가능
indexPath.row / indexPath.section
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "memoCell") else {
return UITableViewCell()
}
Table 자체의 cell이 많아지는 경우, 당장 유저에게 보여지는 cell보다 필요한 cell의 갯수가 많은 경우가 대다수이다. 이때 모든 cell을 미리 준비해놓는 것이 아니라 화면에 보여지는 셀들만큼만 준비를 해놓고 유저가 화면을 스크롤하는 경우 이들을 재활용해서 사용하게 된다.
헤더, 셀의 배치를 하는 경우 점선이 위치하는 경우가 생김, 약간의 여백 → 애플이 준수하라고 하는 가이드, 최소한의 여백 이정도는 남겨라~
배열에 추가 → 테이블뷰에 업데이트 시키기 위해서 tableView.reloadData() (왜냐하면 테이블뷰는 뷰가 로드될때 한 번 다 디자인돼서 가만히 있기 때문) 갱신해주세요~ 요청하는 것
var height: Int? = 160
if let myHeight = height {
print("My height is \(myHeight)")
} else {
print("I don't know")
}
var height: Int? = 160
guard let myHeight = height else {print("I don't know")}
let cell = tableView.dequeueReusableCell(withIdentifier: "memoCell")
if indexPath.section == 0 {
cell?.textLabel?.text = "첫번째 섹션입니다 - \(indexPath)"
}
이때 cell과 textLabel 모두 옵셔널 타입이므로 옵셔널 체이닝으로 값을 받아오고 있다.
guard let cell = tableView.dequeueReusableCell(withIdentifier: "memoCell") else {
return UITableViewCell()
}
if indexPath.section == 0 {
cell.textLabel?.text = "첫번째 섹션입니다 - \(indexPath)"
}
위와 동일한 코드지만 guard문을 사용하여 조금 더 안전하게 cell을 사용할 수 있다.