2022. 10. 30. 23:26ㆍiOS
이글에서는 스토리보드가 아닌 코드로 작업을 진행하였습니다.
하나의 테이블 뷰에서 다중 셀 작업을 진행하려고 합니다.
private lazy var tableView: UITableView = {
let tableView = UITableView()
view.addSubview(tableView)
return tableView
}()
우선 먼저 테이블 뷰 생성합니다.
func tableViewLayout() {
view.addSubview(tableView)
tableView.translatesAutoresizingMaskIntoConstraints = false
tableView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true
tableView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor).isActive = true
tableView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor).isActive = true
tableView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor).isActive = true
tableView.delegate = self
tableView.dataSource = self
}
테이블 뷰 레이아웃을 진행하고 델리겟과 데이터 소스를 추가로 진행합니다.
익스텐션 부분에서 추가되는 부분이 나타날 것입니다. 여기서 가 중요합니다!!
extension ThreeViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if section == 0 {
return dataSource.count(더미데이터입니다.)
} else if section == 1 {
return dataSourceSecond.count(더미데이터입니다.)
}
sction 부분에 접근하여 먼저 셀의 칸별로 나눌 수 있게 작업을 진행합니다.
if 문을 사용하여 칸별을 나눌 수가 있습니다.!
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.section == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: "FisrtCell", for: indexPath) as! FisrtCell
cell.bind(model: dataSource[indexPath.row])
cell.accessoryType = .disclosureIndicator
return cell }
0번째(1번)의 칸의 값을 넣어줄 값을 입력합니다.
} else if indexPath.section == 1 {
let cell = tableView.dequeueReusableCell(withIdentifier: "SecondCell", for: indexPath) as! SecondCell
cell.bind(model: dataSource1[indexPath.row])
cell.accessoryType = .disclosureIndicator
return cell }
1번째(2번)의 칸의 값을 넣어줄 값들을 입력해줍니다.
return UITableViewCell()
}
이런 식으로 뷰가 나타날 겁니다.!!
첫 사진처럼 추가적인 셀을 만들기 위해서는 section의 값을 0,1이 아닌 2,3,4 이렇게 추가로 접근한다면 셀을 나눠 작업이 가능합니다.!
1번째 값인 고객지원 셀을 클릭하면 이렇게 또 안에서 추가적으로 셀을 나누어 작업이 가능합니다.!! (추가적으로 블로그에 글을 쓰도록 하겠습니다.)
[func numberOfSections(in tableView: UITableView) -> Int { return }] 여기서 작업을 진행합니다.!!
추가적으로...
간단할 수도 있겠지만.
셀의 높이의 값을 정할 수 있는 함수입니다.!!
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return int 값
ex) return70 이렇한다면 셀의 높이가 70으로 고정이 됩니다.
}
'iOS' 카테고리의 다른 글
IntrinsicContentSize 에 대해서 알아보기 (2) | 2022.11.20 |
---|---|
기본적인 Userdefaults 사용 하기. (0) | 2022.11.08 |
JSON Decoder 대해 알아보자! (0) | 2022.09.06 |
Codable 을 알아보자! (0) | 2022.09.06 |
동기 // 비동기 대해 알아보자! (0) | 2022.08.14 |