일반적으로 앱의 설정 화면에서 유용하게 쓰일 수 있는 UI 이다.
각 Cell은 악세사리 타입을 가지고 있고, 그 중 disclosure Indicator 를 선택하면 >
모양의 인디케이터를 만들 수 있다.
그리고 여기에 세부 정보를 보여주는 Text Label을 붙이고 싶었다.
즉, UITableViewCell의 구성을 더 풍부하게 만들 수 있는 설정이 필요했고, 그때 사용하는 메서드가 defaultContentConfiguration()
이다. iOS14 이상에서 지원한다.
해당 메서드를 호출하면 cell 설정을 위한 객체가 생성된다. 그 중 secondaryText
속성을 사용하면 별도로 UILabel와 서브뷰를 생성하지 않고도 위 이미지와 같은 설정 페이지를 구성할 수 있다.
아래는 위 이미지에 대한 예제 코드.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: .value1, reuseIdentifier: "cell")
var config = cell.defaultContentConfiguration()
switch indexPath.row {
case 0:
config.text = "알림 관리"
config.secondaryText = "매일"
case 1:
config.text = "기본 리마인더 시간"
config.secondaryText = "9:00 오전"
case 2:
let switchView = UISwitch()
config.text = "앱 내 알림 표시"
cell.accessoryView = switchView
default:
break
}
cell.contentConfiguration = config
cell.accessoryType = .disclosureIndicator
return cell
}