6월 13일 (월)

apwierk·2022년 6월 13일
0

TIL

목록 보기
19/33

TIL (Today I Learned)

6월 13일 (월)

학습 내용

  • 20개의 section, 3개의 row를 갖는 TableView를 만들었다.
class TableViewController: UITableViewController {
    
    override func numberOfSections(in tableView: UITableView) -> Int {
        return 20
    }
    
    override func tableView(_ tableView: UITableView,
                            numberOfRowsInSection section: Int) -> Int {
        return 3
    }
    
    override func tableView(_ tableView: UITableView,
                            cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "basicStyleCell", for: indexPath)
        tableView.rowHeight = 200
        cell.textLabel!.text = "\(indexPath.section) , \(indexPath.row)"
        return cell
    }
    
    override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        tableView.sectionHeaderHeight = 10
        tableView.backgroundColor = .systemGray6
        return "  "
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let alertController = UIAlertController(title: "어머나", message: "날 만졌어 \n Section \(indexPath.section) , Row \(indexPath.row)", preferredStyle: .alert)
        let confirmAction = UIAlertAction(title: "그랬구나", style: .default, handler: nil)
        alertController.addAction(confirmAction)
        self.present(alertController, animated: false)
    }
}

DataSource

  • 20개의 sections와 3개의 row를 설정하는 DataSource코드
override func numberOfSections(in tableView: UITableView) -> Int {
        return 20
    }
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 3
    }
  • 각 row에 들어갈 text를 정의하고, 크기를 수정한다.
 override func tableView(_ tableView: UITableView,
                            cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "basicStyleCell", for: indexPath)
        tableView.rowHeight = 200
        cell.textLabel!.text = "\(indexPath.section) , \(indexPath.row)"
        return cell
    }
  • 각 section마다 머릿말을 넣어준다.
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        tableView.sectionHeaderHeight = 10
        tableView.backgroundColor = .systemGray6
        return " "
    }

Delegate

  • TabelView의 값이 선택되었을 때 액션, 이벤트 발생한다.
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let alertController = UIAlertController(title: "어머나", message: "날 만졌어 \n Section \(indexPath.section) , Row \(indexPath.row)", preferredStyle: .alert)
        let confirmAction = UIAlertAction(title: "그랬구나", style: .default, handler: nil)
        alertController.addAction(confirmAction)
        self.present(alertController, animated: false)
    }

JSON

  • 경로찾기: Bundle.main.path(forResource: 파일명, ofType: 파일타입, inDirectory: 파일경로(상위 폴더이름)) 메소드를 이용하여 상수에 값을 넣어준다. -> 상수에 값을 넣어줄 때 옵셔널 바인딩을 이용한다.
  • JSON파일 내부 값 String으로 꺼내오기: try? String(contentsOfFile: ) 메소드를 사용하여 JSON내부의 값을 String값으로 꺼내준다. 이 과정에서도 또한 옵셔널 바인딩을 이용한다.
  • String을 Data로 변경하기: .data(using: .utf8)메서드를 이용하여 String값을 Data타입으로 변경해주었습니다.
  • 디코딩하기: JSONDecoder()로 상수를 선언하고 그 상수를 이용하여 decoding해준다.
let decoder = JSONDecoder()
var itemList: [Item]?
itemList = try 
do{
	let itemList = decoder.decode([Item].self, from: data)
} catch{
	print(error)
}

일기

새시 (새로운 시작) 오좋 (오히려 좋아!)
윙빙과 짝이 되어서 좋다😎

profile
iOS 꿈나무 개발자

0개의 댓글