Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- NavigationSearchBar
- @escaping
- Value Type Reference Type
- wil
- Array vs Linked List
- 면접을 위한 CS전공 지식 노트
- CarouselCollectionview
- 자료구조
- 프로그래머스
- 면접을 위한 CS 전공 지식 노트 Tree
- UIKit
- retain cycle
- TableView Section
- Input Output
- UserDefaults
- 강한 참조 순환
- TableView
- til
- CoreData
- SWIFT
- Reference Cycle
- ReferceCycle
- coremotion
- 양궁대회
- 테이블뷰 나누기
- 롤케이크 자르기
- class struct
- firebase
- tableview section별 다른 cell적용
- Carousel CollectionView
Archives
- Today
- Total
개발하는 동글 :]
[TIL],[UIKit],[TableView],[섹션 분리 후 다른 Cell적용] 본문
적용 화면
ViewDidLoad()
@IBOutlet weak var myTableView: UITableView!
var dataManager = DataManager()
override func viewDidLoad() {
super.viewDidLoad()
myTableView.dataSource = self
}
테이블 뷰 속성을 만들어 주고 , datasource를 자신으로 할당해 준다.
extension
extension ViewController: UITableViewDataSource, UITableViewDelegate{
// Section 갯수 지정
func numberOfSections(in tableView: UITableView) -> Int {
return dataManager.section.count
}
//var section = ["0","1","2"]
// tableView Cell 갯수 지정, 여기서 Section에 따른 구분
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if section == 0 {
return dataManager.section0Data.count
} else if section == 1 {
return dataManager.section1Data.count
} else {
return dataManager.section2Data.count
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.section == 0 {
let cell = myTableView.dequeueReusableCell(withIdentifier: "TableViewUserInfoCell", for: indexPath) as! TableViewUserInfoCell
return cell
} else if indexPath.section == 1 {
let cell = myTableView.dequeueReusableCell(withIdentifier: "TableViewDefalutCell", for: indexPath) as! TableViewDefalutCell
cell.cellLabel.text = dataManager.section1Data[indexPath.row].name
cell.cellImageView.image = UIImage(named: dataManager.section1Data[indexPath.row].image)
cell.cellInfoLabel.text = dataManager.section1Data[indexPath.row].info
return cell
} else {
let cell = myTableView.dequeueReusableCell(withIdentifier: "TableViewDefalutCell", for: indexPath) as! TableViewDefalutCell
cell.cellLabel.text = dataManager.section2Data[indexPath.row].name
cell.cellInfoLabel.text = dataManager.section2Data[indexPath.row].info
return cell
}
}
}
tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int 메서드를 section에 따라 몇 개의 cell이 나올지 결정해 주고 tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 메서드를 분기처리하여 각각의 section에서 어떠한 cell이 사용될지를 결정한다.